Sunday, November 4, 2018

Node.js

There are few steps we need to do to conveniently working on Node.js.

Install Node.js from its official website. This is the result in my Windows.


Since I'm a big fan of Eclipse, I want to be able to write Node.js code on Eclipse.To do that we installed an Eclipse package nodeclipse.


Next we install express framework using npm (Node Package Manager). Express framework is used to create a Node.js server. Actually we can create a Node.js server without express framework, but since everybody is doing it I think we will just do it :D


Now we will install nodemon using npm. Nodemon is a tool that restarts Node.js server when a file changes. By default, a changes will not be updated if the server is running. Without nodemon we have to restart it everytime we make changes.


Now it's time to create an Eclipse project. Choose to create a simple empty project. Two important files are created there, package.json and server.js. Package.json contains project information while server.js contains the Node.js server code.


Modify the package.json. Make sure the package.json contains "script"."start" "nodemon server.js" which means when we start Node.js server we use nodemon instead of just starting the server. This way when a file changes we don't need to restart the server. "dependencies"."express" and "devDependencies"."nodemon" must also exist.


Modify server.js as shown below.


Start it as shown below. If we modify server.js the server is restarted automatically.


0 comments:

 

©2009 Stay the Same | by TNB