Node.Js错误监听器必须是一个函数

我正在尝试构build一个端点/订单….在哪里可以发出一个POST请求。

var http = require('http'); var options = { hostname: '127.0.0.1' ,port: '8080' ,path: '/order' ,method: 'GET' ,headers: { 'Content-Type': 'application/json' } }; var s = http.createServer(options, function(req,res) { res.on('data', function(){ // Success message for receiving request. // console.log("We have received your request successfully."); }); }).listen(8080, '127.0.0.1'); // I understand that options object has already defined this. req.on('error', function(e){ console.log("There is a problem with the request:\n" + e.message); }); req.end(); 

我得到一个错误“侦听器必须是一个函数”….当试图从命令行运行它 – “节点sample.js”

我希望能够运行这个服务,并卷入它。 有人可以certificate我的代码,并给我一些基本的方向,我去哪里错了吗? 以及如何改进我的代码。

http.createServer()不会将options对象作为参数。 它唯一的参数是一个监听器,它必须是一个函数,而不是一个对象。

下面是一个非常简单的例子:

 var http = require('http'); // Create an HTTP server var srv = http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('okay'); }); srv.listen(8080, '127.0.0.1');