我想将另一个文件夹或驱动器添加到我的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");
});
将来我希望从内部和外部来源添加更多文件夹,但由于我似乎无法添加一个,这可能会有点延伸...
以下是错误消息。
来自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)
来自控制台的错误消息:
Failed to load resource: net::ERR_CONNECTION_REFUSED