源码为:
const express = require('express');
const expressHandlebars = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser')
const app = express();
const port = process.env.PORT || 3000;
// 使用body-parser中间件解析请求主体
app.use(bodyParser.urlencoded({ extended: false }))
问题
在vscode中使用body-parser中间件,显示bodyParser已被弃用,bodyParser is deprecated.
原因以及解决
Express4.16+已经加入了bodyParser,不需要再require,可直接作为express的方法使用.
body-parser正确使用:
const express = require('express');
const expressHandlebars = require('express-handlebars');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
// 使用body-parser中间件解析请求主体
app.use(express.urlencoded({ extended: false }))