Created
October 25, 2022 09:51
-
-
Save Deeks900/5ba48aa09cb5ae9d561221aefebe7f59 to your computer and use it in GitHub Desktop.
Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
app.listen(3000); | |
app.use(express.json()); //global middleware | |
const authRouter = express.Router(); | |
//On this base route usi this router | |
app.use('/auth', authRouter); //global middleware | |
//function name is middleware | |
authRouter | |
.route('/signup') | |
.get(middleware1, getSignUp, middleware2) | |
.post(postSignUp) //path specific middleware | |
function middleware1(req, res, next){ | |
console.log('middleware1 encountered'); | |
next(); | |
} | |
function middleware2(req, res, next){ | |
console.log('middleware2 encountered'); | |
// next(); | |
console.log("middleware2 ended req response cycle"); | |
res.sendFile('/public/index.html', {root:__dirname}); | |
} | |
function getSignUp(req, res, next){ | |
console.log("get signup called"); | |
next(); | |
// res.sendFile('/public/index.html', {root:__dirname}); | |
} | |
function postSignUp(req, res){ | |
let dataObj = req.body; | |
res.json({ | |
message:"User signed up", | |
data: dataObj | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment