Arvutiteaduse instituut
  1. Kursused
  2. 2022/23 sügis
  3. Veebirakenduse loomine (LTAT.05.004)
EN
Logi sisse

Veebirakenduse loomine 2022/23 sügis

  • 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)
});
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused