0
教程:我想通過localhost打開一個文件,但我不知道我必須在瀏覽器中輸入哪個路徑。是localhost,我的server.js位於哪裏? (對不起,我是新來的編程和節點)node js localhost shal打開一個文件
教程碼
var path = require('path'),
fs = require('fs');
require('http').createServer(function(req, res) {
var file = path.normalize(req.url);
console.log(file);
path.exists(file, function(exists) {
if (exists) {
fs.stat(file, function(err, stat) {
var rs;
if (err) { throw err; }
if (stat.isDirectory()) {
res.writeHead(403);
res.end('Forbidden');
} else {
rs = fs.createReadStream(file);
res.writeHead(200);
rs.pipe(res);
}
});
} else {
res.writeHead(404);
res.end('Not found');
}
})
}).listen(4000);
這很可能是CWD。 – tjameson 2013-03-16 01:48:56
console.log語句不會輸出它正在查找的文件嗎?此外,服務器正在偵聽端口4000,因此您在瀏覽器中將該應用程序定位爲http:// localhost:4000 – bryanmac 2013-03-16 03:39:33