简体   繁体   中英

A ECONNREFUSED error of the node. Js

My Node app is throwing a ECONNREFUSED error. 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. 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

Hope it will help you..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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