Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic static file server in NodeJS

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

  1. 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 ?

  2. 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!

like image 838
slapthelownote Avatar asked Sep 01 '11 08:09

slapthelownote


People also ask

What are static files in node js?

Static files are typically files such as scripts, CSS files, images, etc... that aren't server-generated, but must be sent to the browser when requested. If node. js is your web server, it does not serve any static files by default, you must configure it to serve the static content you want it to serve.

How do I serve a directory in node js?

Here is an example of a script that will serve the files in the current directory: var fs = require('fs'), http = require('http'); http. createServer(function (req, res) { fs. readFile(__dirname + req.


1 Answers

Less is more

Just go command prompt first on your project and use

$ npm install express 

Then write your app.js code like so:

var express = require('express'), app = express(), port = process.env.PORT || 4000;  app.use(express.static(__dirname + '/public')); app.listen(port); 

You would then create a "public" folder where you place your files. I tried it the harder way first but you have to worry about mime types which is just having to map stuff which is time consuming and then worry about response types, etc. etc. etc.... no thank you.

like image 71
King Friday Avatar answered Sep 21 '22 19:09

King Friday