Last active
November 19, 2016 09:47
-
-
Save vigzmv/fcd3a6ed48ed6c4d70d6422c799cf713 to your computer and use it in GitHub Desktop.
Simple HTML(only) Serving Node Server
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
var http = require('http'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var hostname = 'localhost'; | |
var port = 3000; | |
var server = http.createServer(function(req, res) { | |
console.log('Request for ' + req.url + ' by method ' + req.method); | |
if (req.method == 'GET') { | |
var fileUrl; | |
if (req.url == '/') fileUrl = '/index.html'; | |
else fileUrl = req.url; | |
var filePath = path.resolve('./public' + fileUrl); | |
var fileExt = path.extname(filePath); | |
if (fileExt == '.html') { | |
fs.exists(filePath, function(exists) { | |
if (!exists) { | |
res.writeHead(404, { | |
'Content-Type': 'text/html' | |
}); | |
res.end('<html><body><h1>Error 404: ' + fileUrl + ' not found</h1></body></html>'); | |
return; | |
} | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
fs.createReadStream(filePath).pipe(res); | |
}); | |
} else { | |
res.writeHead(404, { | |
'Content-Type': 'text/html' | |
}); | |
res.end('<html><body><h1>Error 404: ' + fileUrl + ' not a HTML file</h1></body></html>'); | |
} | |
} else { | |
res.writeHead(404, { | |
'Content-Type': 'text/html' | |
}); | |
res.end('<html><body><h1>Error 404: ' + req.method + ' not supported</h1></body></html>'); | |
} | |
}) | |
server.listen(port, hostname, function() { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
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
var http = require('http'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var hostname = 'localhost'; | |
var port = 3000; | |
http.createServer(function (request, response) { | |
console.log('request for', request.url); | |
var filePath = '.' + request.url; | |
if (filePath == './') | |
filePath = './index.html'; | |
var extname = String(path.extname(filePath)).toLowerCase(); | |
var contentType = 'text/html'; | |
var mimeTypes = { | |
'.html': 'text/html', | |
'.js': 'text/javascript', | |
'.css': 'text/css', | |
'.json': 'application/json', | |
'.png': 'image/png', | |
'.jpg': 'image/jpg', | |
'.ico': 'image/x-icon', | |
'.gif': 'image/gif', | |
'.wav': 'audio/wav', | |
'.mp4': 'video/mp4', | |
'.woff': 'application/font-woff', | |
'.ttf': 'application/font-ttf', | |
'.eot': 'application/vnd.ms-fontobject', | |
'.otf': 'application/font-otf', | |
'.svg': 'application/image/svg+xml', | |
}; | |
contentType = mimeTypes[extname] || 'application/octect-stream'; | |
fs.readFile(filePath, function(error, content) { | |
if (error) { | |
if(error.code == 'ENOENT'){ | |
fs.readFile('./404.html', function(error, content) { | |
response.writeHead(200, { 'Content-Type': contentType }); | |
response.end(content, 'utf-8'); | |
}); | |
} | |
else { | |
response.writeHead(500); | |
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); | |
response.end(); | |
} | |
} | |
else { | |
response.writeHead(200, { 'Content-Type': contentType }); | |
response.end(content, 'utf-8'); | |
} | |
}); | |
}).listen(port, hostname, function() { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment