Node JS
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. The Node.js framework is mostly used to create server-based applications. The framework can easily be used to create web servers which can serve content to users. There are a variety of modules such as the “http” and “request” module, which helps in processing server related requests in the web server space. Node.js can generate dynamic page content. It can create, open, read, write, delete, and close files on the server. It can collect form data. And it can add, delete, and modify data in database
Applications that can be written using Node.js include, but are not limited to:
- Static file servers
- Web Application frameworks
- Messaging middle ware
- Servers for HTML5 multi player games
Node.js File
Node.js files contain tasks that will be executed on certain events. A typical event is someone trying to access a port on the server. Node.js files must be initiated on the server before having any effect. Node.js files have extension “.js”
Node.js handles a file request:
- Sends the task to the computer’s file system.
- Ready to handle the next request.
- When the file system has opened and read the file, the server returns the content to the client.
Module in Node.js
Built-in Modules
assert, buffer, child_process, cluster, crypto, dgram, dns, domain, events, fs, http, https, net, os, path, punycode, querystring, readline, stream, string_decoder, timers, tls, tty, url, util, v8, vm
Include Modules
To include a module, use the require() function with the name of the module:
var http = require(‘http’);
Create Your Own Modules
You can create your own modules, and easily include them in your applications.
The following example creates a module that returns a date and time object:
Example
Create a module that returns the current date and time:
exports.myDateTime = function ()
{
return Date();
};
Use the exports keyword to make properties and methods available outside the module file.
Include Your Own Module
Now you can include and use the module in any of your Node.js files.
The Built-in HTTP Module
Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).
To include the HTTP module, use the require() method:
var http = require(‘http’);
Node.js as a Web Server
The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.
Use the createServer() method to create an HTTP server:
The function passed into the http.createServer() method, will be executed when someone tries to access the computer on port 8080.
Node.js as a File Server
The Node.js file system module allows you to work with the file system on your computer.
To include the File System module, use the require() method:
var fs = require(‘fs’);
Common use for the File System module:
- Read files
- Create files
- Update files
- Delete files
- Rename files
The Built-in URL Module
The URL module splits up a web address into readable parts.
To include the URL module, use the require() method:
var url = require(‘url’);
Parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties:
NPM
NPM is a package manager for Node.js packages, or modules if you like.
Package
A package in Node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project.
Download a Package
Open the command line interface and tell NPM to download the package you want.
I want to download a package called “upper-case”:
Download “upper-case”:
C:\Users\Your Name>npm install upper-case
Now you have downloaded and installed your first package!
NPM creates a folder named “node_modules”, where the package will be placed. All packages you install in the future will be placed in this folder.
Using a Package
Once the package is installed, it is ready to use.
Include the “upper-case” package the same way you include any other module:
var uc = require(‘upper-case’);
Recent Comments