Node.js part II

Hey, are you still here? 😛

On previous post, I tested the node installation and on this post, I’ll invert the steps of the course that I’m doing on Udemy.

At the course, you’ll do some steps and, when you start to thinking “Node is a pain in the ass”, the instructor teach you about “Nodemon”.

If you are a C# Developer, as I am, you’ll didn’t see logic in stop and start the app on every code change, but… Actually, we do “something near this” (I’m not sure if this expression makes sense in English…) all the time, when we compile the code and run the application. We don’t need to restart the IIS because IIS does have an “internal Nodemon” that detects code changes and restart you pool.

When you develop in a company that not allow you to “touch” the server, but are allowed to publish a new website version, if you need to restart the pool to fix anything, you’ll probably type a blank space on web.config and upload it to force the server restart the pool!

It’s not different with node. New version, stop and start the server.

The “Nodemon” makes Node server more like IIS, restarting the server every time you save a JS file.

NPM

At .Net world, we have Nuget, at Node.js, NPM (Node Package Manager)!

At .Net project, Nuget is started automatically, but, at Node project, you’ll need to setup manually.

NPM is installed when you install the NodeJS.

Checking NPM version

Simply run this command: npm version and you’ll see printed at console the information you want.

2017-08-06

Initialize NPM on your project

Run this command: npm init and complete the steps.

2017-08-06 (12)

1st step: name your project. Attention – it must be in lowercase.

2017-08-06 (13)

2nd step: at this moment, accept all defaults! I only put my name as author.

2017-08-06 (14)

3rd step: you’ll get a package.json preview, and, if it’s OK, type yes and return.

2017-08-06 (15)

Finishing, a new file will be created and it’ll have a kind of ‘blue print’ of your app dependencies. Take a look:

2017-08-06 (16)

Installing Nodemon

Cool! Let’s install Nodemon! Run npm install -g nodemon. The option ‘-g’ stands for ‘Global’, so, it’ll be available to all projects!

2017-08-06 (29)

Testing the Nodemon

So, let’s check it out if nodemon do its job! Run nodemon yourfile.js and you’ll see the message you have written before is printed on console.

2017-08-06 (30)

So, let’s modify this message or write another line!

2017-08-06 (31)

When I save the file… Bammm! Look the nodemon make its magic!

2017-08-06 (32)

Well, til now, we have:

  • NPM initialized;
  • Nodemon running!

Next step!

Node.js writing HTML

Restful API, on its definition, can retun HTML (please, see http://restpatterns.mindtouch.us/HTTP_Headers/Accept), even though I personally don’t like this approach, this test is necessary to explain why use Express and at least EJS.

Requiring HTTP module

As we can see in this documentation (https://nodejs.org/api/modules.html),

Node.js has a simple module loading system. In Node.js, files and modules are in one-to-one correspondence (each file is treated as a separate module).

In order to return HTML, you need to use a HTTP server and, at node you must to load the http module (please see the documentation https://nodejs.org/api/http.html).

Let’s code it!

Using the http module is pretty simple and you’ll need it to create a http server!

var http = require(‘http’);

Simple! Now, I have a variable that holds the http module. Let’s create the server!

var server = http.createServer([requestListener]); (please see https://nodejs.org/api/http.html#http_http_createserver_requestlistener).

The requestListener is a function that returns a new instance of the http server, and our code can look like this:

  1. My function has a request and a response, but, at this moment, I only set the response and ignore the request;
  2. My response.end will return only a HTML (see more here https://nodejs.org/api/http.html#http_response_end_data_encoding_callback);
  3. The server will listen to port 80 (see more here https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback).

Here a simple trick! Using the crasis (is the correct word for this sign `?) back tick allows you to break lines and write more readable code!

2017-08-06 (33)

Save and you’ll see the nodemon do its magic and, if is the first time, on windows machines, you’ll need to allow the NodeJS to bypass the firewall.

2017-08-05 (7)

Node is restarted by nodemon…

2017-08-06 (35)

Open the browser and navigate to localhost to see the NodeJS returning the written HTML!

2017-08-06 (34)

Yes… it is not practical… Even if it is possible, doesn’t means that it must be done.

Installing the Express!

Stop the serve and, at console, run: npm install express –save.

The –save option saves the package and make it appear in the project dependencies (see more in https://docs.npmjs.com/cli/install).

2017-08-06 (37)

So, if you download a project and need to restore the packages, simply run npm install and all the packages will be restored.

With Express properly installed, I’ll do the first refactoring! But… The post is too long and, it’ll be the theme for the next post: Express and EJS!

See you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s