I am new to Node Js just started to learn and was trying the following code but there is something wrong in this and it is throwing error.There are similar question available but could not figure out what is wrong in this.
const express = require('express');
var app = express();
const path = require('path');
const PORT = process.env.PORT || 5000;
const logger = require('./middleware/logger')
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const passport = require('passport');
var securedRoutes = express.Router();
//set static folder
app.use(express.static(path.join(__dirname,'public')));
securedRoutes.get('/',(req,res) => {
res.send('Test!!!Vmal');
})
//Init middleware
app.use(logger);
app.use(helmet());
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"]
}
}));
app.use(passport);
app.use(cookieParser());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true,parameterLimit:50000}));
app.listen(PORT,() => console.log(`server started on port ${PORT}`));
module.exports = app;
Error : throw new TypeError('app.use() requires a middleware function') ^TypeError: app.use() requires a middleware function.
Can someone please help on this.Thanks
Since you use passport
module, you have to initialize it when referring from app.
From npm documentation https://www.npmjs.com/package/passport:
...
app.use(passport.initialize());
app.use(cookieParser());
...