javascript - node express 中的 app.post 和 app.use 有什么区别?

标签 javascript node.js curl express

我使用命令 curl -H "Content-Type: application/json"-d '{"name":"sparc_core","port":["p1", "p2"]}' http://127.0.0.1:3000/add_module 测试nodejs服务器。

一开始,我的代码如下:

app.post('/add_module', bodyParser.json()); 
app.post('/add_module', bodyParser.urlencoded());
app.post('/add_module', function(req, res, next) {
    req.body = JSON.parse(req.body.data);
    next();
});
app.post('/add_module', function(req, res) {
    console.log("Start submitting");
    console.log(req.body);
... ...

我运行curl命令后, Node 服务器输出错误信息如下:

SyntaxError: Unexpected token u
at Object.parse (native)
at Object.app.post.res.send.error [as handle] (/home/xtec/Documents/xtec- simict/sim/app.js:80:21)
at next_layer (/home/xtec/Documents/xtec- simict/sim/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:107:5)
at /home/xtec/Documents/xtec- simict/sim/node_modules/express/lib/router/index.js:205:24
at Function.proto.process_params (/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/index.js:269:12)
at next (/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/index.js:199:19)
at next_layer (/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:77:14)
at Object.urlencodedParser [as handle] (/home/xtec/Documents/xtec-simict/sim/node_modules/body-parser/index.js:67:27)
at next_layer (/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:103:13)
POST /add_module 500 7ms - 1021b

然后,我修改代码如下:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.post('/add_module', function(req, res) {
    console.log("Start submitting");
    console.log(req.body);
... ...

我运行相同的 curl 命令,它工作正常!

所以我想知道 app.use 和 app.post 之间的区别。需要您的帮助,非常感谢。

最佳答案

app.use() 用于包含中间件/拦截器函数,该函数将在调用 api 时在实际函数执行之前执行。

更多详情请引用- express 官方website

例如:

app.use(cors());
    app.post("/",function(req,res){
    });

上面这行代码等同于

app.post("/",cors(),function(req,res){
});

app.post , app.get , app.put , app.delete 定义了 api 的 http 方法。
请引用链接http://www.tutorialspoint.com/http/http_methods.htm有关 http 方法的更多详细信息

你的情况

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.post('/add_module', function(req, res) {
    console.log("Start submitting");
    console.log(req.body);
}

当/add_module api 被调用时,首先是 bodyParser.json() 然后是 bodyParser.urlencoded({ 扩展:真 }) 之后调用函数

 function(req, res) {
        console.log("Start submitting");
        console.log(req.body);} 

被称为 . bodyParser.json() 和 bodyParse.urlencoded({extended:true}) 是必需的 从被调用函数(req,res) 中的请求获取正文对象

关于javascript - node express 中的 app.post 和 app.use 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27505363/

相关文章:

javascript - 检查文本字段是否包含javascript中的字母

javascript - 如何将多个中间件功能包装成一个?

javascript - 带有 Node.js 请求模块的多个请求池

java - 使用 CURL 中内容 header 'Applicaiton/Json' 类型的查询参数传递 GET URL

php - 使用 php 检测远程 url 的重定向

javascript - 如何防止div文本溢出容器

javascript - ExtJS checkcolumns 修改模型后无法编辑

c++ - 为arm-linux-gnueabi-gcc交叉编译libcurl

javascript - 使用 One Drive API 的错误 JSON 请求

node.js - typescript 错误 TS2393 : Duplicate function implementation in a Node/Express app