Node Js is one of the most popular and powerful server technologies today. It allows you built the entire website only in one programming Language i.e Javascript. Node js is free and open source server technology that uses Javascript to create complete web software. It runs on various platforms like Windows, Linux, Unix, Mac OS X, etc.
node -v
command to check the installed version of Node Js.CommonJS Modules is the Standard how to code modules are structured. It specifies an ecosystem for JavaScript outside on the server or for native desktop applications.
require('path');
Syntax and usage:
// greet.js var greet=function(){ console.log("hello World"); } module.exports=greet; //In app.js var greet=require('./greet.js'); greet();
var http =require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type':'text/plain'}); res.end('Hello World\n'); }).listen(1320,'127.0.0.3');
Some of the main features of Express JS are listed below: –
Following are the steps used to set up an express JS application: –
This way, an express JS application is set up.
Express JS is an application framework which is light-weighted node JS. A number of flexible, useful and important features are provided by this JavaScript framework for the development of mobile as well as web applications with the help of node JS.
Single-page, multi-page, and hybrid web applications can be built using Express JS.
Express.js is a lightweight web application which helps in organizing the web application into MVC architecture on the server side.
The arguments which are available to an Express JS route handler-function are-
The third argument is optional and may be omitted, but in some cases, it is useful where there is a chain of handlers and control can be passed to one of the subsequent route handlers skipping the current one.
In Express JS, there are two ways for configuring the properties:
There is no notion of any database in Express JS. So, the concept of models is left up to third-party node modules, allowing the users to interface with nearly any type of database.
Since authentication is an opinionated area which is not ventured by express JS, therefore any authentication scheme can be used in express JS for the authentication of users.
Express JS supports any template engine that conforms to the (path, locals, callback) signature.
There’s no need to render HTML with the res.render () function. If there’s a specific file, then you should use the res.sendFile () function. If any assets are being served from a dictionary, then express.static () middleware function needs to be used.
Below are the few reasons why to use Express with Node.js
Http defines a set of request methods to perform the desired actions. These request methods are:
The main difference between the put and the patch is
Put | Patch |
---|---|
The embedded entity is believed to the modified version of the resources that are deposited on the original server. It is requested to the client to replace the stored is substituted. | In this, the information regarding the way of modifying the original server which has the resources to produce a new version is found. |
At the time of updating the resource, you need to forward full payload as the request. | At the time of updating the resource, you only need to send the parameter of the resource which you want to update. |
Run below command on the terminal to set default node version along multiple installed versions of node. You can list all install versions of the node by running nvm ls
nvm alias default v7.3.0
Use node-uuid package to generate unique UUIDs/ guid in Node Js. Below code demonstrates how to generate it.
var uuid = require('node-uuid'); // Generate a v1 (time-based) id uuid.v1(); // Generate a v4 (random) id uuid.v4();
CORS stands for Cross-Origin Resource Sharing. It a is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin.
Use below code to enable CORS on NodeJS
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
Using Node Js you can build applications like:
libuv is Cross-platform I/O abstraction library that supports asynchronous I/O based on event loops.It is written in C and released under MIT Licence.
libuv support Windows IOCP, epoll(4), kqueue(2), and Solaris event ports. Initially, it was designed for Node.js but later it is also used by other software projects.
Reference : https://en.wikipedia.org/wiki/Libuv
Zlib is Cross-platform data compression library. It was written by Jean-loup Gailly and Mark Adler. In Node js, you can Zlib for Threadpool, HTTP requests, and responses compression and Memory Usage Tuning. In order to use zlib in node js, you need to install node-zlib package. After installation below is sample code to use Zlib.
var Buffer = require('buffer').Buffer; var zlib = require('zlib'); var input = new Buffer('lorem ipsum dolor sit amet'); var compressed = zlib.deflate(input); var output = zlib.inflate(compressed);
Further Reading https://nodejs.org/api/zlib.html
Following are the list of few new Features introduced in ES6
JIT stands for Just-in-time. A JIT compiler is a program which is used to send bytecode (it consists of instruction that can be interpreted) to the processor by converting it into instruction. After you have done with writing a program, the compiler compiles the source language statements into bytecode instead of compiling it into the code that carries the information which is similar to the specific hardware platform's processor.
Relation of JIT with Node: Virtual machine of Nodejs has JIT compilation which improves the execution speed of the code. The virtual machine takes the source code and converts to machine code in runtime. By this, the hot functions which are called very often are compiled to machine code and, hence increasing speed.
Aggregations are a set of functions that are used to manipulate the data that has been returned from a MongoDB query. In Mongoose, aggregations work as a pipeline. The aggregate function accepts the array of data transformations which are applied by data using different methods in terms of arguments.
Syntax: db.customers.aggregate([ ... aggregation steps go here ...]);
In above, aggregation is applied to data of customers.
Normally NodeJs reads the content of a file in non-blocking, asynchronous way. Node Js uses its fs core API to deal with files. The easiest way to read the entire content of a file in nodeJs is with fs.readFile method. Below is sample code to read a file in NodeJs asynchronously and synchronously.
Reading a file in node asynchronously/ non-blocking
var fs = require('fs'); fs.readFile('DATA', 'utf8', function(err, contents) { console.log(contents); }); console.log('after calling readFile');
Reading a file in node asynchronously/blocking
var fs = require('fs'); var contents = fs.readFileSync('DATA', 'utf8'); console.log(contents);
Streams are special types of objects in Node that allow us to read data from a source or write data to a destination continuously. There are 4 types of streams available in Node Js, they are
Further reading: https://www.tutorialspoint.com/nodejs/nodejs_streams.htm
In Node Js all core modules, as well as most of the community-published modules, follows a pattern where the first argument to any callback handler is an error object. this object is optional, if there is no error then in that case null or undefined is passed to the callback.
Example of the callback function
function callback(err, results) { // usually we'll check for the error before handling results if(err) { // handle error somehow and return } // no error, perform standard callback handling }
Revealing module pattern is similar to Module Pattern.IT ExposES only the properties and methods you want via an returned Object.
var greeting = 'Hello world' function greet() { console.log(greeting) } module.exports = { greet: greet }
A Closure is a function defined within another scope that has access to all the variables within the outer scope.
Global variables can be made local (private) with closures.
Never Miss an Articles from us.
The Node.js is one the most powerful and popular server-side programming language. It is built on Ch...
Multer is a middleware for node.js that processes multipart/form-data. It is basically used for uplo...
In this tutorial, we are going to see how to Upload an image from URL using Node Js. We have written...