首页 > Web开发 > 详细

learning nodejs 1

时间:2014-01-18 01:34:18      阅读:371      评论:0      收藏:0      [点我收藏+]
a simple http server using inner http module.
bubuko.com,布布扣
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);
bubuko.com,布布扣

learning nodejs 1

原文:http://www.cnblogs.com/kylindai/p/3524607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!