reading-notes

View project on GitHub

What Is Node and When Should I Use It?

What Is Node.js?

Node.js is an open source server environment ,Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Node.js uses JavaScript on the server

Why Node.js?

A common task for a web server can be to open a file on the server and return the content to the client.

Here is how PHP or ASP handles a file request:

Sends the task to the computer’s file system. Waits while the file system opens and reads the file. Returns the content to the client. Ready to handle the next request. Here is how 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. Node.js eliminates the waiting, and simply continues with the next request.

Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.

What Can Node.js Do?

Node.js can generate dynamic page content Node.js can create, open, read, write, delete, and close files on the server Node.js can collect form data Node.js can add, delete, modify data in your database

What is a 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”

Download Node.js

The official Node.js website has installation instructions for Node.js: https://nodejs.org

Getting Started

Once you have downloaded and installed Node.js on your computer, let’s try to display “Hello World” in a web browser.

Create a Node.js file named “myfirst.js”, and add the following code:

myfirst.js

var http = require(‘http’);

http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type’: ‘text/html’}); res.end(‘Hello World!’); }).listen(8080); Save the file on your computer: C:\Users\Your Name\myfirst.js

The code tells the computer to write “Hello World!” if anyone (e.g. a web browser) tries to access your computer on port 8080.

Command Line Interface

Node.js files must be initiated in the “Command Line Interface” program of your computer.

How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for “Command Prompt”, or simply write “cmd” in the search field.

Navigate to the folder that contains the file “myfirst.js”, the command line interface window should look something like this:

C:\Users\Your Name>_

Node.js NPM

What is NPM?

NPM is a package manager for Node.js packages, or modules if you like.

www.npmjs.com hosts thousands of free packages to download and use.

The NPM program is installed on your computer when you install Node.js

What is a 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.