如何在Node.js中接受POST数据

我正在使用NowJS向使用NowJS客户端发送通知,但是我需要在通知中发送的数据必须来自我的数据库。

由于我使用的是Django后端,我可以向我的Node.js服务器发送HTTP请求并发送所需的数据。 但是我需要能够使用Node.js来接受这些数据。 我怎样才能做到这一点?

我是使用Connect / Express的粉丝,所以这里可以使用一个简单的例子:

 var express = require('express'); var app = express.createServer(); var nowjs = require("now"); var everyone = nowjs.initialize(app); app.use(express.bodyParser()); //If you want nice JSON parsing app.post('/sendNotification', function(req, res){ console.log(req.params) //Look at the POST params console.log(req.body) //Look at the POST body everyone.now.receiveMessage(req.body.dataYouCareAbout); res.send('Notification Sent!'); }); 

然后,您可以使用该“sendNotification”端点来接受POST数据,并将其发送到NowJS(或其中的某个部分,或者其他任何部分)。

我怎样才能做到这一点?

 require("http").createServer(function (req, res) { doStuff(); }).listen(PORT); 

使用强大的 。 以下是他们的文档中的一个简短例子:

 var formidable = require('formidable'), http = require('http'), util = require('util'); http.createServer(function(req, res) { if (req.url == '/upload' && req.method.toLowerCase() == 'post') { // parse a file upload var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: files})); }); return; }