简体   繁体   English

Node.js 客户端应用程序在 http 请求期间崩溃

[英]Node.js client app crashes during http request

I'm new to node.js;我是 node.js 的新手; I am reading a book and following it word for word.我正在读一本书并逐字逐句地跟踪它。 I have recently run into a problem and I do not know how to solve it.我最近遇到了一个问题,我不知道如何解决它。 The book asks me to create a client app, I have the code which matches the book's exactly but doesn't run.这本书要求我创建一个客户端应用程序,我有与这本书完全匹配但没有运行的代码。 Below is the code:下面是代码:

fiboclient.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:包.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"
   }
}

output:输出:

> 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

any help is appreciated.任何帮助表示赞赏。

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

i was stuck in same tutorial also same problem , i found the solution in ( https://stackoverflow.com/users/714143/tedmeftah ) replay , he said must run server then the client.我被困在同一个教程中,也遇到了同样的问题,我在 ( https://stackoverflow.com/users/714143/tedmeftah ) 回放中找到了解决方案,他说必须先运行服务器,然后才是客户端。 1 - in /fibonacci> npm run server. 1 - 在 /fibonacci> npm 运行服务器。 2 - in /fibonacci> npm run client. 2 - 在 /fibonacci> npm 运行客户端。 enter image description here在此处输入图片说明

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

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