我正在尝试配置我的节点应用程序,并且当前显示以下错误:
错误:大多数中间件(如bodyParser)不再与Express捆绑在一起,必须单独安装。请参阅https://github.com/senchalabs/connect#middleware。
我当时在网站上,并且不确定我应该寻找什么,因为我对Node.js很陌生。感谢您的帮助!
这是完整的消息。
[0]错误:大多数中间件(如bodyParser)不再与Express捆绑在一起,必须单独安装。请参阅https://github.com/senchalabs/connect#middleware。 [0]在Function.get(/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/express.js:110:13) app.post上的[0](/Users/thomashunt/Desktop/react_express_starter/server.js:45:19) [0]在Layer.handle [作为handle_request](/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/layer.js:95:5) 下一个[0](/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/route.js:137:13) [0]在Route.dispatch(/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/route.js:112:3) [0]在Layer.handle [作为handle_request](/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/layer.js:95:5) [0]位于/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:281:22 [0]在Function.process_params(/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:335:12) 下一个[0](/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:275:10) [0]在urlencodedParser(/Users/thomashunt/Desktop/react_express_starter/node_modules/body-parser/lib/types/urlencoded.js:82:7)
//结束
服务器js文件
const nodemailer = require('nodemailer')
const path = require('path')
const express = require('express')
const app = express()
const port = 5000
const cors = require('cors')
app.use(cors())
const bodyParser = require('body-parser')
app.use(bodyParser.json())
// to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true
})
)
app.get('/home', (req, res) => {
console.log(
'Hello from .get /home',
req.body.generalDetails,
req.body.firstName,
req.body.mName
)
})
const http = require('http');
app.post('/home', (req, res) => {
var server = http.createServer(app)
server.listen(3000, function () {
console.log('Express is running on port 3000')
})
app.use(express.bodyParser());
app.post('/formProcess', function (req, res) {
var data=req.body;
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "email",
pass: "pass"
}});
smtpTransport.sendMail({ //email options
from: "thomas.hunt@careertrackers.org.au",
to: "thomas.hunt@careertrackers.org.au", // receiver
subject: "Emailing with nodemailer", // subject
text:req.body.generalDetails,
html: "here your data goes"// body (var data which we've declared)
}, function(error, response){ //callback
if(error){
console.log(error);
}else{
console.log("Message sent: " + res.message);
}
smtpTransport.close();
}); });
let data = [{
//page one data
generalDetails: req.body.generalDetails,
fName: req.body.fName,
mName: req.body.mName,
lName: req.body.lName,
email: req.body.email,
gender: req.body.gender,
}];
res.json(data);
}
);
app.listen(port, () => `Server running on port ${port}`);
答案 0 :(得分:1)
您已经以正确的方式在应用程序顶部定义了中间件:app.use(bodyParser.json());
您还需要在代码中再定义一次,这是不正确的方式,因为最新版本的express没有随附用于正文解析器的内置中间件。
删除此行:app.use(express.bodyParser());