我创建了一个BasicWebServer.js
文件,该文件位于文件夹portfolio-website
中,该文件夹包含以下文件:index.htm
,BasicWebServer.js
,css/style.css
。
我放入BasicWebServer.js文件中的代码如下:
var http = require('http'),
fs = require('fs'),
path = require('path'),
host = '127.0.0.1',
port = '9000';
var mimes = {
".htm" : "text/html",
".css" : "text/css",
".js" : "text/javascript",
".gif" : "image/gif",
".jpg" : "image/jpeg",
".png" : "image/png"
}
var server = http.createServer(function(req, res){
var filepath = (req.url === '/') ? ('./index.htm') : ('.' + req.url);
var contentType = mimes[path.extname(filepath)];
// Check to see if the file exists
fs.exists(filepath, function(file_exists){
if(file_exists){
// Read and Serve
fs.readFile(filepath, function(error, content){
if(error){
res.writeHead(500);
res.end();
} else{
res.writeHead(200, { 'Content-Type' : contentType});
res.end(content, 'utf-8');
}
})
} else {
res.writeHead(404);
res.end("Sorry we could not find the file you requested!");
}
})
res.writeHead(200, {'content-type' : 'text/html'});
res.end('<h1>Hello World!</h1>');
}).listen(port, host, function(){
console.log('Server Running on http://' + host + ':' + port);
});
更新1
第1部分)
我删除了两行:
res.writeHead(200, {'content-type' : 'text/html'});
res.end('<h1>Hello World!</h1>');
当我刷新页面时,我得到:
Sorry we could not find the file you requested!
第2部分)
我也尝试通过以下方法进行管道输送:
var http = require('http'),
fs = require('fs'),
path = require('path'),
host = '127.0.0.1',
port = '9000';
var mimes = {
".htm" : "text/html",
".css" : "text/css",
".js" : "text/javascript",
".gif" : "image/gif",
".jpg" : "image/jpeg",
".png" : "image/png"
}
var server = http.createServer(function (req, res) {
var filepath = (req.url === '/') ? ('./index.htm') : ('.' + req.url);
var contentType = mimes[path.extname(filepath)];
// Check to see if the file exists
fs.exists(filepath, function(file_exists){
// if(file_exists){
// // Read and Serve
// fs.readFile(filepath, function(error, content){
// if(error){
// res.writeHead(500);
// res.end();
// } else{
// res.writeHead(200, { 'Content-Type' : contentType});
// res.end(content, 'utf-8');
// }
// })
res.writeHead(200, {'Content-Type' : contentType});
var streamFile = fs.createReadStream(filepath).pipe(res);
streamFile.on('error', function() {
res.writeHead(500);
res.end();
})
} else {
res.writeHead(404);
res.end("Sorry we could not find the file you requested!");
}
})
}).listen(port, host, function(){
console.log('Server Running on http://' + host + ':' + port);
});
这给出了以下错误:
SyntaxError: Unexpected token else
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
最佳答案
这两行:
res.writeHead(200, {'content-type' : 'text/html'});
res.end('<h1>Hello World!</h1>');
每次都在执行,它们在代码有机会读取文件之前被执行。请记住,
fs.exists()
和fs.readFile()
是异步的,因此它们的响应将在其余代码执行后出现。如果我删除了这两行,您的代码确实开始工作,因此基本上您在其他代码有机会实际读取并发送文件之前已经完成了这两行的响应。
仅供参考,使用
fs.exists()
的使用方式被认为是反模式。如果找不到该文件,则可以使用fs.readFile()
并适当地处理该错误。除了没有并发问题外,这还少了一个文件操作。