a simple http server using inner http module.
var http = require('http');
var fs = require('fs');

// 这是一个很有趣的包 require(
'colors'); var server = http.createServer(function(req, res) { if ('GET' == req.method && '/images' == req.url.substr(0, 7) && '.jpg' == req.url.substr(-4)) { console.log(req.url.red); console.log((__dirname + req.url).red); fs.stat(__dirname + req.url, function(err, stat) { if (err || ! stat.isFile()) { res.writeHead(404); res.end('Not found'); return; } serve(__dirname + req.url, 'image/jpg'); }); } else if ('GET' == req.method && '/' == req.url) { serve(__dirname + '/index.html', 'text/html'); } else { res.writeHead(404); res.end('Not found'); } function serve(path, type) { console.log(path.yellow); console.log(type.red); res.writeHead(200, {'Content-Type': type});

// 这个是亮点,流的管道方法,类似C++的 << fs.createReadStream(path).pipe(res); } }).listen(
3000);

 

 

相关文章:

  • 2021-07-17
  • 2021-10-08
  • 2021-11-16
  • 2022-02-03
  • 2021-10-02
  • 2021-05-27
  • 2021-09-26
  • 2021-09-25
猜你喜欢
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-06-07
  • 2021-12-31
  • 2021-12-29
相关资源
相似解决方案