简体   繁体   English

节点的ECONNREFUSED错误。 JS

[英]A ECONNREFUSED error of the node. Js

My Node app is throwing a ECONNREFUSED error. 我的Node应用程序抛出ECONNREFUSED错误。 The port should not be in use. 该端口不应该被使用。 Any ideas? 有任何想法吗?

console.info('-----http-----');
console.info();

var http = require('http');
var options = {
    hostname: 'localhost',
    port: 6860,
    path: '/',
    method: 'post'
};

var req = http.request(options, function(res) {
    console.log('status:' + res.statusCode);

    res.setEncoding('UTF-8');
    res.on('data', function(chunk) {
        console.log('body:' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request:' + e.message);
});

req.end();

在此处输入图片说明

Seems you are trying to send post request to one of the URL (localhost), the code you posted that alone will not work, somewhere your server should run i.,e localhost:6860 For that just you need to create a server which runs on the port 6860. Execute this simple server.js file in separate terminal then run "localhost:6860" in your browser. 似乎您正在尝试将发布请求发送到其中一个URL(localhost),单独发布的代码将无法正常工作,您的服务器应该在某个地方运行,例如localhost:6860为此,您只需要创建一个可以运行的服务器在6860端口上。在单独的终端上执行此简单的server.js文件,然后在浏览器中运行“ localhost:6860”。 Then run your code in separate terminal, it will execute properly. 然后在单独的终端中运行您的代码,它将正确执行。 Check your both terminals you will get the difference. 检查您的两个终端,您将得到不同。

**server.js**

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    console.log(req.url)
    console.log(req.method)
    res.end('okay');
}).listen(6860, "localhost");

$node server.js $ node server.js

Hope it will help you..! 希望对您有帮助。

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

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