app.post 和 app.use 在节点表达中有什么区别

what's the different between app.post and app.use in nodes express?

本文关键字:app 什么 区别 节点 post use      更新时间:2023-09-26

I use command curl -H "Content-Type: application/json" -d '{"name":"sparc_core","port":["p1", "p2"]}' http://127.0.0.1:3000/add_module 來測試 nodejs server。

起初,我的代码如下:

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 命令后,节点服务器输出错误信息如下:

语法错误:意外的标记 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(
开机自检/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 时执行实际函数之前执行。

欲了解更多详情,请参阅-快捷官方网站

例如:

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 方法的更多详细信息,请参阅链接 http://www.tutorialspoint.com/http/http_methods.htm

在您的情况下

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}( 是必需的 get body 对象 从被调用函数(req,res( 中的请求中获取正文对象

它完全一样,但是:

  • 应用程序中包含的中间件将在所有请求中使用

  • 包含在 app.post("/route" 中的中间件将仅用于与路径/route 匹配的 POST 类型的请求

例如,如果您的服务器包含以下内容:

 // Common middleware
 app.use(function(req, res, next){
    console.log("Middleware1");
    return next();
 });
 app.use(function(req, res, next){
    console.log("Middleware2");
    return next();
 });
 // POST middleware
 app.post("/api/test1", function(req, res, next){
    console.log("Middleware3");
    return next();
 })
 app.post("/api/test2", function(req, res, next){
    console.log("Middleware4");
    return next();
 })

 // Actions
 app.post("/api/test1", function(req, res, next){
    console.log("finalPOSTAction1");
    return res.status(200).json({});
 })
 app.post("/api/test2", function(req, res, next){
   console.log("finalPOSTAction2");
   return res.status(200).json({});
 })
 app.get("/api/test3", function(req, res, next){
   console.log("finalGETAction3");
   return res.status(200).json({});
 })

在/api/test3 上的请求 GET 将引发以下内容:

- Middleware1
- Middleware2
- finalGETAction3

在/api/test1 上的请求 POST 将引发以下内容:

- Middleware1
- Middleware2
- Middleware3
- finalPOSTAction1

在/api/test2 上的请求 POST 将引发以下内容:

- Middleware1
- Middleware2
- Middleware4
- finalPOSTAction2