Express.js Routing and Middleware
const express = require('express');
const app = express();//It can be included directly in the Route request
function steps(req, res, next){
console.log("first step")
next()
}
app.get("/first", steps ,(req, res)=>{
res.send("end stack") //first step, end stack
})//Aplied to all requests routes
app.use(steps)
app.get("/ruota", (req, res)=>{
console.log("This is the return") //first step, this is the return
res.send("how does a middleware work")
})
//This middleware will be ignored.
app.use("/ruota", (req, res, next)=>{
console.log("This is the second")
next()
})//Invoking them in the array order
function primabase(req, res, next){
next()
}
function secondabase(req, res, next){
console.log("Per primo " + req.params.secondo + req.params.primo)
next()
}
let basi = [secondabase, primabase]
app.get("/linea/:primo/:secondo", basi, (req, res, next)=>{
console.log("This is the last part")
res.send("end of the wave")
})Independent routing with express.router()
Password hashing with bycryptjs
PreviousNodeJs1: Server request/responses with Postman, CORS, and JWT authentificationNextCORS implementation
Last updated