0

I want to add another folder or drive to my Node.js server, but as I'm a noobie with Node.js I just can't wrap my head around it. Here's what I've done so far, but it keeps crashing. Tried several versions of the code below, but it just doesn't seem to work.

var http = require("http");
var fs = require("fs");
var server = http.createServer(function(request, response) {    
    fs.readFile("C:/public" + request.url, function(error, data) {
            response.writeHead(200, {"Content-type":"text/html"});
            fs.readFile("D:/images" + request.url, function(error, data) {
                response.writeHead(200, {"Content-Type":"image/jpg"});
                response.write(data);
            }); 
            response.end(data);
    });
});
server.listen(1337, "127.0.0.1", function() {
    console.log("Listening on port 1337");
});

In the future I'd love to add even more folders from both internal and external sources, but as I can't seem to even add one, that's probably going to be a bit of a stretch...

Here are the error messages.

Error message from the cmd prompt:

Request received: /index.html

http.js:851
    throw new TypeError('first argument must be in a string or Buffer');
          ^
TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.write (http.js:851:11)
    at C:\serverpath\script.js:31:14
    at fs.js:107:20
    at Object.oncomplete (fs.js:107:15)

Error message from Console:

Failed to load resource: net::ERR_CONNECTION_REFUSED 
Kebman
  • 1,901
  • 1
  • 20
  • 32
  • Can you paste the crash error log ? – andersevenrud May 08 '14 at 22:00
  • 1
    to read from multiple folders you need to create some sort of loop using an Array and create a custom function for reading the requested file. – andersevenrud May 08 '14 at 22:03
  • Also, this code does not look right. You also have some security issues here. If you are new to Node.js there is some good reading material in the answers here: http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js – andersevenrud May 08 '14 at 22:07
  • Thanks. I added the crash errors. Otherwise the code works just fine when I don't try to add another folder. – Kebman May 08 '14 at 22:11
  • Writing a web server on Node is not that straightforward! Have you tried using Express? Adding paths (or "folders") is simpler with it. – Jorge Aranda May 09 '14 at 03:42
  • I haven't. Though I really want to learn Node.js well outside a framework first. – Kebman May 09 '14 at 21:21

1 Answers1

1

You are probably looking for a library like this:

https://github.com/caolan/async

It will allow you to do async operations over an Array.

Edit: Found this answer https://stackoverflow.com/a/9449669/1236086

Community
  • 1
  • 1
andersevenrud
  • 419
  • 4
  • 11
  • Thank you. So if I wanted to do this without a library, what would I have to do? – Kebman May 08 '14 at 22:16
  • @Kebman I have updated this answer with a link for more information. It should give you an idea of how to do this – andersevenrud May 08 '14 at 22:18
  • What do you think about making different modules that add inn other drives and folders? Or possibly pass it in parametrically? – Kebman May 08 '14 at 22:23
  • @Kebman I'm not quite sure what you mean. You can just define some sort of Array you can add and remove paths/drives (as you wish) for lookup. – andersevenrud May 08 '14 at 22:27