How to Install NODE-JS ?

NODE-JS

Using Website:
You can visit on the link Download Node and download LTS version.
After installing node you can check your node version in command prompt using command..

~ $node --version

After that, you can just create a folder and add a file here for example app.js. To run this file you need to execute command…

first app $node app.js

Node Modules: There are some built-in modules which you can use to create your applications. Some of the popular modules are- OS, fs, events, HTTP, URL and then you can include these modules in your file using these lines.

var fs = require('fs');

Here is an example of how to include HTTP module to build the server…
filter_none
brightness_4

var http = require('http'); 

// Create a server object: 
http.createServer(function (req, res) { 

// Write a response to the client 
res.write('mycodetips'); 

// End the response 
res.end(); 

// The server object listens on port 8080 
}).listen(8080);

This will listen to server on port 8080. Once you will run your file in command prompt it will execute your file and listen to the server on this port. You can also create your own module and include in your file.

Using NPM: NPM is a Node Package Manager provides packages to download and use. It contains all the files and modules that you require in your application. To install any package you need to execute command…

npm install

I am going to show an example of using Events module.

filter_none
brightness_4

var events = require('events'); 
var eventEmitter = new events.EventEmitter(); 

// Create an event handler: 
var myEventHandler = function () { 

console.log('Welcome tomycodetips'); 
} 

// Assign the event handler to an event: 
eventEmitter.on('mycodetips', myEventHandler); 

// Fire the 'mycodetips' event: 
eventEmitter.emit('mycodetips');

So this is how you can start with node and build your own applications. There are some frameworks of the node which you can use to build your applications. Some popular frameworks of node are…Express.js, Socket.io, Koa.js, Meteor.js, Sail.js.


Discover more from CODE t!ps

Subscribe to get the latest posts sent to your email.

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading