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