0

So I have this custom middleware that tries to get the cookie from the client. This is the declaration:

function auth(req, res, next) {}

But to access the cookies from the request, I need to use another middleware i.e. cookieParser. So now I have no ideas how can I use that cookieParser in my custom auth Middleware. Any help will be greatly appreciated.

1 Answer 1

2

You do not use the middleware in your middleware. See the code bellow which should be fairly self-explanatory but i add some commments.

var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();

// add the cookie parser
app.use(cookieParser());

// your middleware
app.use(function auth(req, res, next) {
    // the cookie middleware above has processed the cookies
    // you access them like this

    console.log(req.cookies);
});

app.get('/', ...);

app.listen(8080)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I am new to the backend stuff and couldn't think of how to make it work. Finally, I got it. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.