node.js Error: connect ECONNREFUSED; response from server

前端 未结 6 993
再見小時候
再見小時候 2020-12-09 03:52

I have a problem with this little program:

var http = require(\"http\");
var request = http.request({
    hostname:          


        
相关标签:
6条回答
  • 2020-12-09 04:25

    I had the same problem on my mac, but in my case, the problem was that I did not run the database (sudo mongod) before; the problem was solved when I first ran the mondo sudod on the console and, once it was done, on another console, the connection to the server ...

    0 讨论(0)
  • 2020-12-09 04:25

    If you have stopped the mongod.exe service from the task manager, you need to restart the service. In my case I stopped the service from task manager and on restart it doesn't automatically started.

    0 讨论(0)
  • 2020-12-09 04:27

    use a proxy property in your code it should work just fine

    const https = require('https');
    const request = require('request');
    
    request({
        'url':'https://teamtreehouse.com/chalkers.json',
        'proxy':'http://xx.xxx.xxx.xx'
        },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var data = body;
                console.log(data);
            }
        }
    );
    
    0 讨论(0)
  • 2020-12-09 04:30

    Please use [::1] instead of localhost, and make sure that the port is correct, and put the port inside the link.

    const request = require('request');
    
       let json = {
            "id": id,
            "filename": filename
        };
        let options = {
            uri: "http://[::1]:8000" + constants.PATH_TO_API,
            // port:443,
            method: 'POST',
            json: json
        };
        request(options, function (error, response, body) {
            if (error) {
                console.error("httpRequests : error " + error);
            }
            if (response) {
                let statusCode = response.status_code;
                if (callback) {
                    callback(body);
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-09 04:31

    i ran the local mysql database, but not in administrator mode, which threw this error

    0 讨论(0)
  • 2020-12-09 04:34

    From your code, It looks like your file contains code that makes get request to localhost (127.0.0.1:8000).

    The problem might be you have not created server on your local machine which listens to port 8000.

    For that you have to set up server on localhost which can serve your request.

    1. Create server.js

      var express = require('express');
      var app = express();
      
      app.get('/', function (req, res) {
        res.send('Hello World!'); // This will serve your request to '/'.
      });
      
      app.listen(8000, function () {
        console.log('Example app listening on port 8000!');
       });
      
    2. Run server.js : node server.js

    3. Run file that contains code to make request.

    0 讨论(0)
提交回复
热议问题