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 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
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
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:
Create a module that returns the current date and time:
exports.myDateTime = function ()
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 is a package manager for Node.js packages, or modules if you like.
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”:
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’);