Skip to content

Instantly share code, notes, and snippets.

@nanha
Last active December 16, 2015 21:48
Show Gist options
  • Save nanha/5501942 to your computer and use it in GitHub Desktop.
Save nanha/5501942 to your computer and use it in GitHub Desktop.
nodeqa.com 답변 nodejs 기본 route 방법
var http = require('http'), path = require('path'), fs = require('fs');
http.createServer(function(req, res) {
var file = path.normalize('.' + req.url);
path.exists(file, function(a) {
if (a === false) {
res.writeHead(404);
res.end('Not found');
return;
}
res.writeHead(200);
var a = fs.createReadStream(file);
//
// 이 부분에서 확장자를 판단하여 writeHead 에 mime-type을 기입하여야 한다.
// 참조 https://github.com/broofa/node-mime
// 그러나, 지정하지 않아도 브라우져에 따라 렌더링 될 수 있다.
//
a.pipe(res);
});
}).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment