Last active
December 15, 2015 11:07
-
-
Save marlun78/5b6b6271798ffbc5f57c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* server.js | |
* Copyright (c) 2015 marlun78 | |
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83 | |
* Requires Node version >= 4.x | |
*/ | |
'use strict'; | |
const fs = require('fs'); | |
const http = require('http'); | |
const path = require('path'); | |
const DEFAULT_FILE = process.env.DEFAULT_FILE || 'index.html'; | |
const ROOT = process.env.ROOT || process.cwd(); | |
const PORT = process.env.PORT || 9988; | |
const MIME_TYPE = { | |
// Application | |
JSON: 'application/json', | |
// Image | |
GIF: 'image/gif', | |
ICO: 'image/ico', | |
JPG: 'image/jpg', | |
JPEG: 'image/jpeg', | |
PNG: 'image/png', | |
// Text | |
CSS: 'text/css', | |
HTML: 'text/html', | |
JS: 'text/javascript', | |
TXT: 'text/plain' | |
}; | |
const NODE_ERROR = { | |
NO_SUCH_FILE_OR_DIRECTORY: 'ENOENT' | |
}; | |
const STATUS_CODE = { | |
OK: 200, | |
NOT_FOUND: 404, | |
INTERNAL_SERVER_ERROR: 500 | |
}; | |
const STATUS_MESSAGE = { | |
OK: '200 OK', | |
NOT_FOUND: '404 Not Found', | |
INTERNAL_SERVER_ERROR: '500 Internal Server Error' | |
}; | |
const log = { | |
writeln: console.log.bind(console) | |
}; | |
const server = http.createServer(requestListener); | |
server.listen(PORT, listening); | |
function requestListener(request, response) { | |
let absolutePath = resolveAbsolutePath(request.url); | |
fs.readFile(absolutePath, (error, data) => { | |
if (error) { | |
handleError(response, error); | |
} else { | |
handleSuccess(response, absolutePath, data); | |
} | |
}); | |
} | |
function resolveAbsolutePath(url) { | |
let relativePath = url.replace(/\?.*/, ''); | |
if (relativePath.endsWith('/')) { | |
relativePath += DEFAULT_FILE; | |
} | |
return path.join(ROOT, relativePath); | |
} | |
function handleSuccess(response, file, data) { | |
log.writeln(STATUS_CODE.OK, file); | |
response.writeHead(STATUS_CODE.OK, createHeaders(findMimeType(file))); | |
response.end(data); | |
} | |
function handleError(response, error) { | |
switch (error.code) { | |
case NODE_ERROR.NO_SUCH_FILE_OR_DIRECTORY: | |
log.writeln(STATUS_CODE.NOT_FOUND, error.toString()); | |
response.writeHead(STATUS_CODE.NOT_FOUND, createHeaders(MIME_TYPE.TXT)); | |
response.end(STATUS_MESSAGE.NOT_FOUND); | |
break; | |
default: | |
log.writeln(STATUS_CODE.INTERNAL_SERVER_ERROR, error.toString()); | |
response.writeHead(STATUS_CODE.INTERNAL_SERVER_ERROR, createHeaders(MIME_TYPE.TXT)); | |
response.end(STATUS_MESSAGE.INTERNAL_SERVER_ERROR); | |
} | |
} | |
function findMimeType(filePath) { | |
let ext = path.extname(filePath).substr(1).toUpperCase(); | |
return MIME_TYPE[ext]; | |
} | |
function listening() { | |
console.log('Server started on', PORT); | |
} | |
function createHeaders(contentType) { | |
return { | |
'Content-Type': contentType | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment