Getting started with nodeJS on Windows

NodeJs allow you to write server side javascript. If you've been a webdevelopper with javascript  and javascript libraries like Jquery, and working mostly in classic web agencies, this short article will get you throught the crucial first steps to installing and running a nodeJS application.

 

NodeJS is a program based on the Google Chrome web browser embarked javascript Engine. Until now, you mostly see javascript on browser and not on desktop application for example.

Some smart folk had the idea to use the javascript engine of Google Chrome ( called V8) as a standalone program to run javascript.

Now you can use Node to run a webserver (Yes it replaces Apache or IIS), or to run javascript programs on Windows or Linux. Github.com has written an IDE that runs on nodeJS. It is called Atom. More and more website are run on nodeJS server, it has a smaller memory usage than Apache, so it is very lightweight.

In the field of Internet of Things, we start to see people run Iot device in javascript where we traditionally use C language program.

But enough said, i am going to show you how to install NodeJs and start to run scripts.

 

 

Then nodeJs comes with a package manager npm which the central component for writing your server side javascript project.

NodeJs by itself is a small program, and the folks that maintain it use a modular approach to loading libraries.

There are so many libraries you just can't download on your computer. Pne more reason this approach is chosen is because the libraries are rarely frozen, there are regular updates. So this modular approcah will ensure you have the latest version of a library any time, and you don't have to download them manually, npm does it for you with a simple command.

Install nodeJS

First you must install the nodeJs program, you can download it on the official website.

After you installed nodeJS, you are ready to write your firs serverside javascript !

Create a folder, and a file test.js and put inside console.log('Hello World');

Save it and open a terminal, go to the folder and type:

$  node test.js

You'll see :

Hello World!

That's it ! you've written your first nodejs file !

Now let's go a step further, nodeJS is most reknowned for being a webserver. In our cas we want to run nodejs and set it to listen to http request.

 

Your first local node server

 Rename your test.js to server.js

put this code inside

var http =require('http');
var fs =require('fs');
var url =require('url');
// Create a server

http
.createServer(function(request, response){
// Parse the request containing file name

var
pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console
.log("Request for "+ pathname +" received.");
// Read the requested file content from file system

fs
.readFile(pathname.substr(1),function(err, data){if(err){ console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response
.writeHead(404,{'Content-Type':'text/html'});}else{
//Page found
// HTTP Status: 200 : OK

// Content Type: text/plain

response
.writeHead(200,{'Content-Type':'text/html'});
// Write the content of the file to response body

response
.write(data.toString());}
// Send the response body

response
.end();});}).listen(3000);
// Console will print the message

console
.log('Server running at http://127.0.0.1:3000/');


Create an index.html file with the hello world string inside the same directory.

Now run the node server again :

$ node server.js

navigate on your browser to 127.0.0.1:3000 and there you go !

 

We've seen that we can create a minimalistic website with nodeJS. But you may have heard of ExpressJS. ExpressJS is a minimalistic framework to perform all thing relative to http.

  1. It has a routing system to map url to page content
  2. It allows a dynamic web page rendering similar to MVC parameterized url
  3. Allows to setup middleware to respond to http request

 In a next chapter i will introduce you to ExpressJS

Posté le 2015-12-05 17:12:45


0 Commentaires


Fermé aux commentaires