🔖 [Nodejs] Introduction to Nodejs

2018 - 04 - 01
🔖 [Nodejs] Introduction to Nodejs
There are many reasons that make Nodejs hot these days. It is easy to learn, based on javascript, browser compliance, asynchronous, etc. First, we better understand the origins and role of Nodejs.
0. [Origins of Nodejs] Browsers have their own engines built with programming lanauges in order to interpret another language written in ECMAScript format. The most popular ECMAScript we have is JavaScript, that's why we often heard of ES6, ES7, ES8, etc. In order to run a page with JavaScript, browsers use their engines to interpret the page line by line. The names of engines for popular browsers are:
  • Safari - Nitro
  • Chrome - V8
  • Firefox - SpiderMoneky
Among these browsers, Chrome is the most common so programmers built another thing called Nodejs based on V8 engines. With Nodejs, JavaScript can be interpreted more efficiently and used for more functions. So basically, Nodejs is not a programming languages, it is not a framework such as Django or Ruby.
1. [Installation] Nodejs is used on terminal of our MacOS, in order to let terminal understand the command we have to install Nodejs from their official website Nodejs.org Once installed, we can check the version of current Nodejs using terminal:
$node --version
To test if Nodejs is running normally, we can build a dummy.js file:
console.log('Hello World!')
And run this file using terminal:
$node dummy
On your terminal it should looks like:
2. [Require()] In JavaScript, we have function declared like this:
function myFunc(){
	do something...;	
};
If we have this myFunc in a file named second.js and wanna call this function from first.js, it should be firstly exported from second.js to first.js as a part of the module: second.js
function myFunc(){
	do something...;
};
module.exports.myFunc = myFunc; //this will export second.js as an object with an object method named myFunc
first.js
const second = require('./second.js'); //require the object second.js
second.myFunc();
Alternatively, we can call the function directly without creating an object:
function myFunc(){
	do something...;
};
module.exports = myFunc; //this will export the function directly without creating an object
first.js
const myFunc = require('./second.js');
myFunc();

3. [EventEmitter] In Nodejs, events are passed to a thread to handle in a loop. Events is a class that emit an event. To emit an event we can define a class named EventEmitter, such as
const EventEmitter = require('events');
let emitter = new EventEmitter(); //this line is here to show that EventEmitter is a class not an object
This example show how emitter works: second.js
const EventEmitter = require('events');

class Emitter extends EventEmitter{
	myFunc(){
		console.log(1+1);
		this.emit('abc', {id:1, user:'john'});
	};
};

module.exports = Emitter;
first.js
const Second = require('./second');
let second = new Second();
//Register an event listener
second.on('abc', e=>{
	console.log(e);
});

//Running the class method
second.myFunc();

4. [http module] http is the module in Nodejs that we used to create server. Its createServer() is also an EventEmitter, such that it has .on(), .emit(), .addListener() as we mentioned. Although it is the basic way to run a HTTP server in Nodejs, we do not use it in real world as it is so slow. Instead, there are some frameworks can help such as Express. Still we can learn about this basic http module: first.js:
const http = require('http');
const server = http.createServer((request, response)=>{
	if(request==='/'){
		response.write('Hello!');
		response.end();
	};
});

server.listen(8000);
now run this file on terminal:
$node first
and open Chrome browser and enter url as localhost:8000, you'll see something like this:

Comments

There is no comment yet

New Comment

Please Login to comment