Skip to content

Instantly share code, notes, and snippets.

@kuju63
Created November 19, 2016 16:47
Show Gist options
  • Save kuju63/2a283c3469e623d615c44aa31f2983f2 to your computer and use it in GitHub Desktop.
Save kuju63/2a283c3469e623d615c44aa31f2983f2 to your computer and use it in GitHub Desktop.
簡易Webサーバ
//Web サーバーが Listen する IP アドレス
var LISTEN_IP = '127.0.0.1';
//Web サーバーが Listen する ポート
var LISTEN_PORT = 8080;
//ファイル名が指定されない場合に返す既定のファイル名
var DEFAULT_FILE = "index.html";
var http = require('http'),
fs = require('fs');
//拡張子を抽出
function getExtension(fileName) {
var fileNameLength = fileName.length;
var dotPoint = fileName.indexOf('.', fileNameLength - 5 );
var extn = fileName.substring(dotPoint + 1, fileNameLength);
return extn;
}
//content-type を指定
function getContentType(fileName) {
var extentsion = getExtension(fileName).toLowerCase();
var contentType = {
'html': 'text/html',
'htm' : 'text/htm',
'css' : 'text/css',
'js' : 'text/javaScript; charset=utf-8',
'json' : 'application/json; charset=utf-8',
'xml' : 'application/xml; charset=utf-8',
'jpeg' : 'image/jpeg',
'jpg' : 'image/jpg',
'gif' : 'image/gif',
'png' : 'image/png',
'mp3' : 'audio/mp3',
};
var contentType_value = contentType[extentsion];
if(contentType_value === undefined){
contentType_value = 'text/plain';};
return contentType_value;
}
function getFile(requestUrl) {
if (requestUrl.indexOf('?') > -1) {
var paramIndex = requestUrl.indexOf('?');
var url = requestUrl.substring(0,paramIndex);
return url;
} else {
return requestUrl;
}
}
//Web サーバーのロジック
var server = http.createServer();
server.on('request',
function(request, response){
console.log('Requested Url:' + request.url);
var requestedFile = request.url;
requestedFile = (requestedFile.substring(requestedFile.length - 1, 1) === '/')?requestedFile + DEFAULT_FILE : getFile(requestedFile);
console.log('Handle Url:' + requestedFile);
console.log('File Extention:' + getExtension( requestedFile));
console.log('Content-Type:' + getContentType( requestedFile));
fs.readFile('.' + requestedFile,'binary', function (err, data) {
if(err){
console.log('Error:' + err);
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('not found\n');
response.end();
}else{
response.writeHead(200, {'Content-Type': getContentType(requestedFile)});
response.write(data, "binary");
response.end();
}
});
}
);
server.listen(LISTEN_PORT, LISTEN_IP);
console.log('Server running at http://' + LISTEN_IP + ':' + LISTEN_PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment