Building your own web server with nodejs in 5 minutes
Well, it took me almost a week to get back to my blog :)
Couple of things I want to add to my blog before I forget! I mentioned about NPM, the node package manager that helps you to get the new modules. So how do I request for a new module using npm?
Psst! I stumbled upon this blog http://blog.modulus.io/absolute-beginners-guide-to-nodejs which got cool things that you can do with node.js. And I am installing express module (of course, using npm) to build my own web server.
I have my web project 'npj' created under: C:\Development. So basically 'npj' is a folder under C:\Development that has all my JavaScript, CSS, images folders and files.
Now request for the module you are looking for:
C:\Development>npm install express
npm is going to install the express module in there, that's it!
Now, let's go ahead and create a web site and browse it using your own web server that you can build with node. Create a file nodeWebServer.js under C:\Development.
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/npj'));
app.listen(8080);
Four lines of code and my server is ready! Run my server as below:
C:\Development>node nodeWebServer.js
Note: Now you are using node.js to run your newly created web server. So node.js is busy running it so you CAN NOT access it from command prompt any more unless you stop your web server.
No comments:
Post a Comment