var http = require('http') var parse = require('url').parse var fs = require('fs') var join = require('path').join
var root = __dirname var server = http.createServer(function(req, res){ var url = parse(req.url) var path = join(root, url.pathname) var stream = fs.createStream(path) stream.on('data',function(chunk){ res.write(chunk) }) stream.on('end',function(){ res.end() }) })
var server = http.createServer((req,res)=>{ var parse = require('url').parse var join = require('path').join var path = join(__dirname,parse(req.url).pathname)
fs.stat(path,(err,stat)=>{ if(err){ if('ENOENT' == err.code){ res.status = 404 res.end('file not found') }else{ res.status = 500 res.end('Internet Server error') } }else{ res.setHeader('Content-Length',stat.size) var stream = fs.createReadStream(stat) stream.pipe(res) stream.on('error',(err)=>{ res.statusCode = 500; res.end('Internet server error') }) } }) })