A simple static file server built with Node.js 1

A simple static file server built with Node.js

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 mimeTypes = {
        '.html': 'text/html',
        '.js': 'text/javascript',
        '.css': 'text/css',
        '.json': 'application/json',
        '.png': 'image/png',
        '.jpg': 'image/jpg',
        '.gif': 'image/gif',
        '.svg': 'image/svg+xml',
        '.wav': 'audio/wav',
        '.mp4': 'video/mp4',
        '.woff': 'application/font-woff',
        '.ttf': 'application/font-ttf',
        '.eot': 'application/vnd.ms-fontobject',
        '.otf': 'application/font-otf',
        '.wasm': 'application/wasm'
    };

    var 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(404, { 'Content-Type': 'text/html' });
                    response.end(content, 'utf-8');
                });
            }
            else {
                response.writeHead(500);
                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');
        }
    });

}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');

Here is what the above code is Doing:
1. We are creating a server using the http module.
2. We are then reading the requested file and returning the content to the client.
3. If the file is not found, we are returning a custom 404 page.
4. If there is any other error, we are returning a custom 500 page.

Now, let’s create the index.html file.




Hello World

Hello World


Now, let’s create the 404.html file.




404

404


Now, let’s run the server.

$ node server.js
Server running at http://127.0.0.1:8125/

Now, open the browser and go to http://127.0.0.1:8125/. You should see the following output:

Hello World

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now, go to http://127.0.0.1:8125/test.html. You should see the following output:

404

Now

Similar Posts