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');
- PhantomJS Web服务器与节点?
- 找不到bind.gyp(c:\ xampp \ htdocs \ webapp)而在Windows XAMPP中加载binding.gyp
- 什么是适合我的Firefox版本的geckodriver?
- 运行“grunt”命令时出现“致命错误:无法find本地grunt”
- Express – 处理json响应
- Express中app.use('*')和app.all('*')之间的区别
- 当通过github进行部署时,快照版本在nodejitsu中不会改变
- Node.js主机名/ IP不匹配证书的名称
- JavaScript(NodeJS)等价于PHP的call_user_func_array()