简体   繁体   English

Node.js获取JSON发布数据

[英]Node.js get json post data

I use express.js on my server. 我在服务器上使用express.js。 From my client I try to: 从我的客户,我尝试:

$http.post("url/send", angular.toJson(
    {
        uploads: uploads, 
        desc: desc
    }
));

On the server I want to read this data: 在服务器上,我想读取以下数据:

send function(req, res, next){

};

How can I extract the posted json string from the req object? 如何从req对象中提取发布的json字符串?

In Express add bodyParser middleware in configure: 在Express中,在configure中添加bodyParser中间件:

app.configure(function() {
  app.use(express.bodyParser());
});

And then in any request, req.body will contain your JSON with body data: 然后在任何请求中, req.body将包含带有主体数据的JSON:

app.post('/items', function(req, res, next) {
  console.log(req.body);
});

You need to add the bodyParser in your express setup like this 您需要像这样在您的快速设置中添加bodyParser

app.configure(function () {
    app.use(express.bodyParser({ keepExtensions: true }));
});

Then in your route/middleware u just reed the data in req.body 然后在您的路由/中间件中,您只需要req.body中的数据

The above solutions have been deprecated in Express 4. Note that configure is no longer used to set up middleware. 上面的解决方案已在Express 4中弃用。请注意,configure不再用于设置中间件。 Secondly, bodyParser is no longer part of Express. 其次,bodyParser不再是Express的一部分。 Instead bodyParser is its own entity package and should be called separately https://www.npmjs.com/package/body-parser the code: 相反,bodyParser是其自己的实体包,应单独调用https://www.npmjs.com/package/body-parser的代码:

    app.use(express.bodyParser());
    });

is in Express 4: 在Express 4中:

app.use(bodyParser()); 

(much simpler!) (简单得多!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM