我是Node Js的新手,刚开始学习并尝试下面的代码,但是这样做有问题,并且引发错误。有类似的问题可以解决,但是无法找出问题所在。
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;
错误:抛出新的TypeError('app.use()需要中间件功能') ^ TypeError:app.use()需要一个中间件功能。
有人可以帮忙吗?谢谢
答案 0 :(得分:3)
由于使用了passport
模块,因此从应用中引用时必须初始化。
从npm文档https://www.npmjs.com/package/passport:
...
app.use(passport.initialize());
app.use(cookieParser());
...