【问题标题】:Node.js client app crashes during http requestNode.js 客户端应用程序在 http 请求期间崩溃
【发布时间】:2017-10-30 02:33:34
【问题描述】:

我是 node.js 的新手;我正在读一本书,并逐字逐句。我最近遇到了一个问题,我不知道如何解决它。这本书要求我创建一个客户端应用程序,我的代码与本书完全匹配,但没有运行。下面是代码:

fiboclient.js:

var http = require('http');
var util = require('util');
[
    "/fibonacci/30", "/fibonacci/20", "/fibonacci/10",
    "/fibonacci/9", "/fibonacci/8", "/fibonacci/7",
    "/fibonacci/6", "/fibonacci/5", "/fibonacci/4",
    "/fibonacci/3", "/fibonacci/2", "/fibonacci/1"
].forEach(path => {
        util.log('requesting ' + path);
        var req = http.request({
                host: "localhost",
                port: 3002,
                path: path,
                method: 'GET'
         }, res => {
                 res.on('data', chunk => {
                         util.log('BODY: ' + chunk);
                 });
         });
         req.end();
});

package.json:

{
  "name": "fibonacci",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "DEBUG=fibonacci:* node ./bin/www",
    "server": "SERVERPORT=3002 node ./fiboserver",
    "client": "node ./fiboclient"
   },
   "dependencies": {
     "body-parser": "~1.17.1",
     "cookie-parser": "~1.4.3",
     "debug": "~2.6.3",
     "ejs": "~2.5.6",
     "express": "~4.15.2",
     "morgan": "~1.8.1",
     "serve-favicon": "~2.4.2"
   }
}

输出:

> fibonacci@0.0.0 client 
/home/zschiff/Dropbox/Personal/Node/ch04/fibonacci
> node ./fiboclient

29 May 11:37:56 - requesting /fibonacci/30
29 May 11:37:56 - requesting /fibonacci/20
29 May 11:37:56 - requesting /fibonacci/10
29 May 11:37:56 - requesting /fibonacci/9
29 May 11:37:56 - requesting /fibonacci/8
29 May 11:37:56 - requesting /fibonacci/7
29 May 11:37:56 - requesting /fibonacci/6
29 May 11:37:56 - requesting /fibonacci/5
29 May 11:37:56 - requesting /fibonacci/4
29 May 11:37:56 - requesting /fibonacci/3
29 May 11:37:56 - requesting /fibonacci/2
29 May 11:37:56 - requesting /fibonacci/1
events.js:163
  throw er; // Unhandled 'error' event
  ^

Error: connect ECONNREFUSED 127.0.0.1:3002
    at Object.exports._errnoException (util.js:1050:11)
    at exports._exceptionWithHostPort (util.js:1073:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)

npm ERR! Linux 4.4.0-78-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "client"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! fibonacci@0.0.0 client: `node ./fiboclient`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the fibonacci@0.0.0 client script 'node 
./fiboclient'.
npm ERR! Make sure you have the latest version of node.js and npm 
installed.
npm ERR! If you do, this is most likely a problem with the fibonacci 
package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./fiboclient
npm ERR! You can get information on how to open an issue for this 
project with:
npm ERR!     npm bugs fibonacci
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls fibonacci
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/zschiff/Dropbox/Personal/Node/ch04/fibonacci/npm-
debug.log

感谢任何帮助。

【问题讨论】:

  • 在不知道服务器端代码的情况下,这很难解决..
  • Error: connect ECONNREFUSED,表示客户端无法连接到127.0.0.1:3002。确保您的服务器在那里运行。
  • 当您运行 node client 时,fiboClient 正在向 127.0.0.1:3002 发出请求。似乎没有服务器在运行。您是否在不同的控制台窗口上运行过node server?没有看到整个代码就很难判断,但似乎那里没有服务器。
  • 谢谢所有这确实是问题!!!!!

标签: javascript json node.js


【解决方案1】:

客户端应用程序 (fiboclient.js) 正在尝试向 localhost (IP 127.0.0.1) 端口 3002 发出请求。错误“connect ECONNREFUSED 127.0.0.1:3002”表明您没有服务器正在监听localhost 端口 3002。检查 fiboserver.js 是否正在运行并监听该端口。

【讨论】:

    【解决方案2】:

    我被困在同一个教程中也同样的问题,我在 (https://stackoverflow.com/users/714143/tedmeftah) replay 中找到了解决方案,他说必须先运行服务器然后再运行客户端。 1 - 在 /fibonacci> npm 运行服务器。 2 - 在 /fibonacci> npm 运行客户端。enter image description here

    【讨论】:

    • 您发布的第一个链接好像坏了。
    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2017-09-28
    相关资源
    最近更新 更多