gpt4 book ai didi

node.js - 连接到 Node “most middleware (like bodyParser) and must be installed separately by nodejs"时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:57:54 27 4
gpt4 key购买 nike

我正在尝试配置我的 Node 应用程序,但当前出现错误:

错误:大多数中间件(如 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)[0] 在 app.post (/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}`);

最佳答案

您已以正确的方式在应用程序顶部定义了中间件:app.use(bodyParser.json());

您还在代码中再次定义了它,这不是正确的方式,因为最新版本的 Express 没有内置用于正文解析器的中间件。

删除此行:app.use(express.bodyParser());

关于node.js - 连接到 Node “most middleware (like bodyParser) and must be installed separately by nodejs"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com