Basic static file server in NodeJS
http, node.js, webserver
Solution
Your basic server looks good, except:
There is a `return` statement missing.
res.write('404 Not Found\n');
res.end();
return; // <- Don't forget to return here !!
And:
`res.writeHead(200, mimeType);`
should be:
`res.writeHead(200, {'Content-Type':mimeType});`
Yes `pipe()` does basically that, it also pauses/resumes the source stream (in case the receiver is slower). Here is the source code of the `pipe()` function: https://github.com/joyent/node/blob/master/lib/stream.js
Problem
I'm trying to create a static file server in nodejs more as an exercise to understand node than as a perfect server. I'm well aware of projects like Connect and node-static and fully intend to use those libraries for more production-ready code, but I also like to understand the basics of what I'm working with. With that in mind, I've coded up a small server.js: ``` var http = require('http'), url = require('url'), path = require('path'), fs = require('fs'); var mimeTypes = { "html": "text/html", "jpeg": "image/jpeg", "jpg": "image/jpeg", "png": "image/png", "js": "text/javascript", "css": "text/css"}; http.createServer(function(req, res) { var uri = url.parse(req.url).pathname; var filename = path.join(process.cwd(), uri); path.exists(filename, function(exists) { if(!exists) { console.log("not exists: " + filename); res.writeHead(200, {'Content-Type': 'text/plain'}); res.write('404 Not Found\n'); res.end(); } var mimeType = mimeTypes[path.extname(filename).split(".")[1]]; res.writeHead(200, mimeType); var fileStream = fs.createReadStream(filename); fileStream.pipe(res); }); //end path.exists }).listen(1337); ``` My question is twofold Is this the "right" way to go about creating and streaming basic html etc in node or is there a better/more elegant/more robust method ? Is the .pipe() in node basically just doing the following? . ``` var fileStream = fs.createReadStream(filename); fileStream.on('data', function (data) { res.write(data); }); fileStream.on('end', function() { res.end(); }); ``` Thanks everyone!