MDN’s new design is in Beta! A sneak peek:

Node server without framework


Though Node has many framework to help you getting your server up and running, mainly:

  • Express: The most used framework
  • Total: The ALL-IN-ONE UNITY framework, that have everything and do not depend on any other framework or module.

But, there is always no one size fits all, so developer may need to build his/her own server, without any other dependancy.


Below is a simple quick static file nodejs server:

var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (request, response) {
    console.log('request ', 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',
        '.gif': 'image/gif',
        '.wav': 'audio/wav',
        '.mp4': 'video/mp4',
        '.woff': 'application/font-woff',
        '.ttf': 'application/font-ttf',
        '.eot': 'application/',
        '.otf': 'application/font-otf',
        '.svg': 'application/image/svg+xml'

    contentType = mimeTypes[extname] || 'application/octet-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.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
        else {
            response.writeHead(200, { 'Content-Type': contentType });
            response.end(content, 'utf-8');

console.log('Server running at');

Document Tags and Contributors

 Contributors to this page: sergey-seleznev, Coco92, image72, hasan3ysf
 Last updated by: sergey-seleznev,