Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
web server in node.js
// A very basic web server in node.js
// Stolen from: Node.js for Front-End Developers by Garann Means (p. 9-10)
var port = 8000;
var serverUrl = "127.0.0.1";
var http = require("http");
var path = require("path");
var fs = require("fs");
var checkMimeType = true;
console.log("Starting web server at " + serverUrl + ":" + port);
http.createServer( function(req, res) {
var now = new Date();
var filename = req.url || "index.html";
var ext = path.extname(filename);
var localPath = __dirname;
var validExtensions = {
".html" : "text/html",
".js": "application/javascript",
".css": "text/css",
".txt": "text/plain",
".jpg": "image/jpeg",
".gif": "image/gif",
".png": "image/png",
".woff": "application/font-woff",
".woff2": "application/font-woff2"
};
var validMimeType = true;
var mimeType = validExtensions[ext];
if (checkMimeType) {
validMimeType = validExtensions[ext] != undefined;
}
if (validMimeType) {
localPath += filename;
fs.exists(localPath, function(exists) {
if(exists) {
console.log("Serving file: " + localPath);
getFile(localPath, res, mimeType);
} else {
console.log("File not found: " + localPath);
res.writeHead(404);
res.end();
}
});
} else {
console.log("Invalid file extension detected: " + ext + " (" + filename + ")")
}
}).listen(port, serverUrl);
function getFile(localPath, res, mimeType) {
fs.readFile(localPath, function(err, contents) {
if(!err) {
res.setHeader("Content-Length", contents.length);
if (mimeType != undefined) {
res.setHeader("Content-Type", mimeType);
}
res.statusCode = 200;
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}
@lili21

This comment has been minimized.

Copy link

lili21 commented Dec 13, 2013

line 37:
getFile(localPath, res, isValidExt);

@marcomontalbano

This comment has been minimized.

Copy link

marcomontalbano commented Jun 26, 2014

I'm agree with oracle-li

@anuragsharmam

This comment has been minimized.

Copy link

anuragsharmam commented Sep 2, 2014

line no 34: path.exists should be fs.exists now

@WandileChamane

This comment has been minimized.

Copy link

WandileChamane commented Jul 18, 2016

Hi not sure where the now date var is used. just omitted it, thanks

@xzhang2016

This comment has been minimized.

Copy link

xzhang2016 commented Jul 27, 2016

Hi, I used your code to make my html file as a server, but the file cannot show properly and the buttons on the html file don't work. This html file is used to test APIs that send commands to another website which is a client of another node server. When I open this html file locally using browser, it works. Could you give me some advice? Thanks!

@parikh5555

This comment has been minimized.

Copy link

parikh5555 commented Jul 29, 2016

Nice One man... Really Good for beginners

@ronanquillevere

This comment has been minimized.

Copy link

ronanquillevere commented Oct 31, 2016

line 37 should be isValidExt instead of ext

getFile(localPath, res, isValidExt);
@hectorcorrea

This comment has been minimized.

Copy link
Owner Author

hectorcorrea commented Nov 10, 2016

Thanks for the feedback. I've updated it to address the path.exists() and isValidExt issues that you all reported.

@kingram6865

This comment has been minimized.

Copy link

kingram6865 commented Jan 3, 2017

My JSHint says: Use '!==' to compare with 'undefined'

@alpersilistre

This comment has been minimized.

Copy link

alpersilistre commented Nov 1, 2017

fs.readFile deprecated. You should use fs.access or fs.stat now.

I have replaced the fs.exists part with fs.access;

fs.access(localPath, function(err) {
    if(err) {
        console.log('File not found: ' + localPath);
        res.writeHead(404);
        res.end();
    }

    console.log('Serving file: ' + localPath);
    getFile(localPath, res, mimeType);
})

https://nodejs.org/api/fs.html#fs_fs_exists_path_callback

@ammarbinfaisal

This comment has been minimized.

Copy link

ammarbinfaisal commented Mar 23, 2018

Thanks a lot
Really helpful for noobs like me

@MalkyTheKid

This comment has been minimized.

Copy link

MalkyTheKid commented Oct 23, 2018

Hi Hector,
I tried the script but I keep on getting "Invalid file extension detected: (/)"

what can I do to resolve this? how do we exactly use the file above?

@thibka

This comment has been minimized.

Copy link

thibka commented Dec 20, 2018

I tried the script but I keep on getting "Invalid file extension detected: (/)"

Same here!

@wyllyjon

This comment has been minimized.

Copy link

wyllyjon commented Apr 10, 2019

Same pb.
I used this code : var filename = req.url == "" || req.url == "/" ? "index.html" : req.url;

@willemavjc

This comment has been minimized.

Copy link

willemavjc commented Apr 23, 2019

In case this might be useful for some, below a more up to date version (as of 2019) of how to serve files with node.js in only 30 lines of code, especially when using node 11.x which deprecated tons of things and now recommends tons of others things.

var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var baseDirectory = __dirname;   // or whatever base directory you want

var port = 8080;

http.createServer(function (request, response) {
    try {
        var requestUrl = url.parse(request.url);

        // need to use path.normalize so people can't access directories underneath baseDirectory
        var fsPath = baseDirectory+path.normalize(requestUrl.pathname);

        var fileStream = fs.createReadStream(fsPath);
        fileStream.pipe(response);
        fileStream.on('open', function() {
            response.writeHead(200);
        });
        fileStream.on('error',function(e) {
            response.writeHead(404) ;    // assume the file doesn't exist
            response.end();
        });
    } catch(e) {
        response.writeHead(500);
        response.end() ;    // end the response so browsers don't hang
        console.log(e.stack);
    }
}).listen(port);

console.log("listening on port "+port);

Obviously, it can be implemented a MIME restriction.

This code is not mine. All credits go to Ole Haugset and B T who did it on Stackoverflow for a beginner.

I checked a bit the code, it runs smoothly on node 11.14.0-alpine, follows some recommandations of the official documentation.
For example:

The fs.readFile() function buffers the entire file. To minimize memory costs, when possible prefer streaming via fs.createReadStream().

@willemavjc

This comment has been minimized.

Copy link

willemavjc commented Apr 25, 2019

Line 61: res.setHeader("Content-Length", contents.length); is incorrect.

It can mislead the actual length computation which depends on the data encoding.
Use Buffer.byteLength(string, [encoding]) to solve this.

Ref: Documentation
Ref: Stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.