Friday, 21 April 2017

npm command creates and error and I can find the fix

I am new to programming node.js. I am following a book and up until now have not had any issues. he newest section has me create a file, and edit the package.json file in order to run a script command. the npm run server command works, but the npm run client command errors out. Below are my files any help would be appreciated.

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();
});

fiboserver.js:

var math = require('./math');
var express = require('express');
var logger = require('morgan');
var app = express();

app.use(logger('dev'));
app.get('/fibonacci/:n', function(req,res,next) {
    math.fibonacciAsync(Math.floor(req.params.n), (err, val) => {
         if (err) next('FIBO SERVER ERROR ' + err);
         else {
             res.send({
                 n: req.params.n,
                 result: val
             });
         }
    });
});
app.listen(process.env.SERVERPORT);

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"
  }
}

the output with npm run client :

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

          21 Apr 22:47:57 - requesting /fibonacci/30
          21 Apr 22:47:57 - requesting /fibonacci/20
          21 Apr 22:47:57 - requesting /fibonacci/10
          21 Apr 22:47:57 - requesting /fibonacci/9
          21 Apr 22:47:57 - requesting /fibonacci/8
          21 Apr 22:47:57 - requesting /fibonacci/7
          21 Apr 22:47:57 - requesting /fibonacci/6
          21 Apr 22:47:57 - requesting /fibonacci/5
          21 Apr 22:47:57 - requesting /fibonacci/4
          21 Apr 22:47:57 - requesting /fibonacci/3
          21 Apr 22:47:57 - requesting /fibonacci/2
          21 Apr 22:47:57 - 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-72-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



via Zachary Schiff