How do I add another folder to my Node.js project?

时间:2021-11-23 10:56:39

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.

我想将另一个文件夹或驱动器添加到我的Node.js服务器,但由于我是Node.js的noobie,我无法绕过它。这是我到目前为止所做的,但它一直在崩溃。试过下面几个版本的代码,但它似乎没有用。

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:

cmd提示符出现错误消息:

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:

来自Console的错误消息:

Failed to load resource: net::ERR_CONNECTION_REFUSED 

1 个解决方案

#1


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.

它允许您对Array执行异步操作。

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

编辑:发现此答案https://*.com/a/9449669/1236086

#1


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.

它允许您对Array执行异步操作。

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

编辑:发现此答案https://*.com/a/9449669/1236086