我正在尝试建立一个端点/订单....可以发出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();
我得到一个错误"监听器必须是一个函数"....当尝试从命令行运行它时- "node sample.js"
我希望能够运行此服务并curl进入它。有人能证明读我的代码,给我一些基本的方向在哪里我走错了?以及如何改进我的代码
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');