Skip to content

Instantly share code, notes, and snippets.

@beaucarnes
Created May 11, 2019 11:05
Show Gist options
  • Save beaucarnes/f5e7c14a246e32df58c01b7e49a940c1 to your computer and use it in GitHub Desktop.
Save beaucarnes/f5e7c14a246e32df58c01b7e49a940c1 to your computer and use it in GitHub Desktop.
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request ', request.url);
var filePath = './public' + request.url;
if (filePath == './public/') {
filePath = './public/index.html';
}
console.log(filePath)
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, function(error, content) {
if (error) {
fs.readFile('./public/404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment