Institute of Computer Science
  1. Courses
  2. 2022/23 fall
  3. Web Application Development (LTAT.05.004)
ET
Log in

Web Application Development 2022/23 fall

  • Home
  • Lectures
  • Practicals
  • Homework Submission
  • Message Board

Setting up a Node.js App to deal with cookies/jwt

To Set up a Node.js project, and install nodemon and Express, please refer to the material from week 11. Therefore, we assume that your project is already there, and the information here will focus on Setting up a Node.js App to deal with cookies/jwt (week 13).

1. If node-postgres (pg) is not installed, you need to install it by writing in the terminal

    > npm install pg

2. If your server is not connected to the database (week 12), you need to connect it by creating a file and naming it database.js. Then, paste the following script into it. Remember to add your password and the name of your database.

// database.js
const Pool = require('pg').Pool;
const pool = new Pool({
    user: "postgres",
    password: "[add your password here]",
    database: "[add the name of your database here]",
    host: "localhost",
    port: "5432"
});

module.exports = pool;

2. If Cross-Origin Resource Sharing (CORS) is not installed, you need to install it by writing in the terminal

  > npm install cors

3. We need also to install bcrypt, which we need to salt and hash passwords, as well as comparing a password entered by a user with a hashed password in the database. You can install bcrypt by writing in the terminal

  > npm install bcrypt

4. We need also to install cookie-parser, which parses Cookie header and populates req.cookies with an object keyed by the cookie names. You can install cookie-parser by writing in the terminal

  > npm install cookie-parser

5. We need also to install jsonwebtoken, which is required for signing and verifying jwt tokens. You can install jsonwebtoken by writing in the terminal

  > npm install jsonwebtoken

6. Now we need to import/require the previous modules/packages into your main js file (server.js). Then, create the related middlewares after the app instance, as follows:

// server.js
const express = require('express');
const pool = require('./database');
const cors = require('cors');
const bcrypt = require('bcrypt');
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');

const port = process.env.PORT || 3000;

const app = express();

app.use(cors({ origin: 'http://localhost:8080', credentials: true }));
// We need to include "credentials: true" to allow cookies to be represented  
// Also "credentials: 'include'" need to be added in Fetch API in the Vue.js App

app.use(express.json());  // Parses incoming requests with JSON payloads and is based on body-parser.
app.use(cookieParser());  // Parse Cookie header and populate req.cookies with an object keyed by the cookie names.

``````````````
    // Code will go here  

``````````````
app.listen(port, () => {
    console.log("Server is listening to port " + port)
});
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment