Tags

, , , ,

I think that this piece of code can be useful during some tests… At least it was useful for me during play with Backbone.js, so this code creates HTTP server and serves files from current directory. If everything is ok status is 200, if file doesn’t exists 404 and if error occur during reading 500.

// static file http server
// serve files for application directory

var fs = require("fs");
var http = require("http");
var url = require("url");

http.createServer(function (req, res) {
	var request = url.parse(req.url, false);
	console.log("Serving request: " + request.pathname);

	var filename = request.pathname.slice(1);

	try {
		fs.realpathSync(filename);
	} catch (e) {
		res.writeHead(404);
		res.end();
	}

	var contentType = "text/plain";

	if (request.pathname.match(".js$")) {
		contentType = "text/javascript";
	} else if (request.pathname.match(".html$")) {
		contentType = "text/html";
	}

	fs.readFile(filename, function(err, data) {
		if (err) {
			res.writeHead(500);
			res.end();
			return;
		}

		res.writeHead(200, {"Content-Type": contentType});
		res.write(data);
		res.end();
	});
}).listen(8000);

console.log("server started");

This code can be obtained also from my GitHub

Advertisements