what is wrong here ?

293 views
Skip to first unread message

Roelof Wobben

unread,
Jan 11, 2015, 11:06:22 AM1/11/15
to nod...@googlegroups.com
I try to make a exercise of nodeschool.io  but I cannot make it work.

I have this :

var http = require ('http');
var fs = require('fs')

http.createServer(function(req, res) {

  var filename = __dirname+ process.argv[3]
  var readStream = fs.createReadStream(filename);

  readStream.on('open', function () {
     readStream.pipe(res);
  });

  readStream.on('error', function(err) {
    res.end(err);
  });
}).listen(process.argv[2]);

then I see this error


http.js:851
    throw new TypeError('first argument must be a string or Buffer');
          ^
TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.write (http.js:851:11)
    at ServerResponse.OutgoingMessage.end (http.js:985:16)
    at ReadStream.<anonymous> (/home/roelof/node_exercises/learnyounode/httpfileserver.js:16:9)
    at ReadStream.emit (events.js:95:17)
    at fs.js:1513:12
    at Object.oncomplete (fs.js:107:15)
✗ Error connecting to http://localhost:40856: socket hang up

when I swap the process.argv then I see this message ;


events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1132:5)
    at Object.<anonymous> (/home/roelof/node_exercises/learnyounode/httpfileserver.js:16:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
✗ Error connecting to http://localhost:5727: connect ECONNREFUSED

Forrest Norvell

unread,
Jan 11, 2015, 1:55:19 PM1/11/15
to nod...@googlegroups.com

What command line are you using? What happens when you add console.log statements saying what values you’re going to pass to each of the library calls you make?

(You should use path.resolve or path.join instead of filename = __dirname + argv[3]__dirname doesn’t come with a trailing slash, among other things.)

F


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7a0d1159-c805-4d6a-9605-25a6d3bc9ff9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roelof Wobben

unread,
Jan 12, 2015, 2:10:26 AM1/12/15
to nod...@googlegroups.com
I use learnyounode run command. and after these tips the problem is solved.

it seems that __dirname does not the work properly.

Roelof


Op zondag 11 januari 2015 19:55:19 UTC+1 schreef Forrest L Norvell:

greelgorke

unread,
Jan 12, 2015, 9:34:23 AM1/12/15
to nod...@googlegroups.com
Hi!

first of all: help for nodeschool workshoper can be found here https://github.com/nodeschool/discussions/issues

secondly: your filename is wrong, as forest said, there is no trailing slash on __dirname. either you add it mnanually or use path.join()

thirdly: you can't do res.end(err). res.end() uses re.write() and write expects a Buffer or a string. so if you want to present the error message to the client you have to call res.end('"ERROR:" + err.message) or something like that.
Reply all
Reply to author
Forward
0 new messages