ExpressJS

Introduction

  • It is a framework that allows user to focus on developing business logic instead of typing boilerplate code for defining route and handling request and response

  • It is all about middleware that the incoming request will go through all the funnels and then send the response back to client side

  • The default query string parser is using qs library

const express = require("express");
const path = require("path");
const app = express();

// define middleware to parse body of request

// usally used for rest api, as the content-type is mainly application/json
// for parsing the request body as json
app.use(express.json());

// used to parse form data, as the content type is mainly 
// application/x-www-form-urlencoded
app.use(express.urlencoded({extended: false});

// override the query parser setting, the default value is "extended"
// however, the default depth of qs is 5, so needed to be increased when needed
app.set('query parser', (queryString) => {
    const qs = require('qs');
    return qs.parse(queryString, {
      depth: 100, // Increase depth limit from default 5
      arrayLimit: 100, // Optional: increase array items limit
      parameterLimit: 2000, // Optional: increase parameter count limit
    });
  });

// server static file, such as image
app.use(express.static(path.join("__dirname","public")));
// defining middleware globally
app.use((req,res,next)=>{
  console.log("middleware 1");
  // go to next middleware
  next();
});

app.use((req,res,next)=>{
  console.log("middleware 2");
  next();
});

// only applicable to "/halo" path
app.use("/halo",(req,res,next)=>{
  console.log("halo");
});

// only applicable to "/halo" path
app.use("/halo/:id",(req,res,next)=>{
  console.log("halo",req.params.id);
});

app.use("/halo",(req,res,next)=>{
  console.log("halo");
});

// only applicable for post method and path "/post"
app.post("/post", (req,res,next)=>{
  console.log(req.body);
});

// The code behind is to create http server, ...
app.listen(8080, function () { 
  console.log("Server Start "); 
});

Separating Routes

  • Express Router allows to divide paths into multiple files

Error Handling

Template Engine

  • The template engine is used to understand the certain syntax, and scan the html-ish template and then replace the placeholder from server

  • Finally, it will generate the html file

Flash message

  • In order to provide user feedback while redirecting the response, flash message will be used

  • The message will be stored into session temporarily, when message has been used, the message will be pop up from the session

CSRF Token

File Handling

Upload

Download

Deployment

Pre-action

  • Get the ubuntu server from Vultr / DigitalOcean / AWS

  • Change Password :sudo passwd

  • Install nodejs and update its version:

  • Install mysql :

  • Install forever / pm2

Https

  • Register the domain in goDaddy or other

  • Link to your ip address

  • Add Certbot PPA

  • Install Certbot

  • Register to obtain the key, cert and ca

  • Read the key ,cert and ca into server.js in production and be https

Forever

  • Install forever to execute the nodejs express program

  • Execute npm command by using forever

  • Find the uid by using:

  • Stop the program by using uid

  • Find the uid who is using the port (e.g 8080)

  • Kill the process by using uid

Last updated

Was this helpful?