我有以下代码使用 node.js 在 static/college.html 中提供网页但它遇到错误并在浏览器中显示“内部服务器错误:无法读取文件”,这是在代码中打印的用于测试的行。
任何建议以及如何解决这个问题?谢谢
// Require the functionality we need to use:
var http = require('http'),
url = require('url'),
path = require('path'),
mime = require('mime'),
path = require('path'),
fs = require('fs');
// Make a simple fileserver for all of our static content.
// Everything underneath <STATIC DIRECTORY NAME> will be served.
var app = http.createServer(function(req, resp){
var filename = path.join(__dirname, "static", url.parse(req.url).pathname);
(fs.exists || path.exists)(filename, function(exists){
if (exists) {
fs.readFile(filename, function(err, data){
if (err) {
// File exists but is not readable (permissions issue?)
resp.writeHead(500, {
"Content-Type": "text/plain"
});
resp.write("Internal server error: could not read file");
resp.end();
return;
}
// File exists and is readable
var mimetype = mime.lookup(filename);
resp.writeHead(200, {
"Content-Type": mimetype
});
resp.write(data);
resp.end();
return;
});
}else{
// File does not exist
resp.writeHead(404, {
"Content-Type": "text/plain"
});
resp.write("Requested file not found: "+filename);
resp.end();
return;
}
});
});
app.listen(3456);
感谢您迄今为止的建议。我只是打印这一行url.parse(req.url).pathname
,但它在那里显示为空。那里可能有什么问题?