+-
var fs = require('fs')
var http = require('http')
var url = require('url');
var path = require('path')
function aa(req,res){
//获取分解后的path的pathname
var pathname = url.parse(req.url).pathname;
fs.readFile(ROOT,path.join(pathname),function (err,file){
if(err){
res.writedHead(404);
res.end("找不到相关文件")
return
}
res.writeHead(200);
res.end(file);
})
}
http.createServer(aa).listen(1337,'127.0.0.1');
打开http://localhost:1337/ ROOT报错
写成这样就行
var fs = require('fs')
var http = require('http')
function handler(req, res) {
fs.readFile(__dirname + req.url, function (err, data) {
if (err) {
res.writeHead(404)
res.end(JSON.stringify(err))
return
}
res.writeHead(200)
res.end(data)
})
}
http.createServer(handler).listen(1337, '127.0.0.1')