Node.
js Introduction
❮ PreviousNext ❯
What is [Link]?
[Link] is an open source server environment
[Link] is free
[Link] runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
[Link] uses JavaScript on the server
Why [Link]?
[Link] uses asynchronous programming!
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:
1. Sends the task to the computer's file system.
2. Waits while the file system opens and reads the file.
3. Returns the content to the client.
4. Ready to handle the next request.
Here is how [Link] handles a file request:
1. Sends the task to the computer's file system.
2. Ready to handle the next request.
3. When the file system has opened and read the file, the server returns the
content to the client.
[Link] eliminates the waiting, and simply continues with the next request.
[Link] runs single-threaded, non-blocking, asynchronous programming, which
is very memory efficient.
What Can [Link] Do?
[Link] can generate dynamic page content
[Link] can create, open, read, write, delete, and close files on the server
[Link] can collect form data
[Link] can add, delete, modify data in your database
What is a [Link] File?
[Link] files contain tasks that will be executed on certain events
A typical event is someone trying to access a port on the server
[Link] files must be initiated on the server before having any effect
[Link] files have extension ".js"
[Link] Get Started
❮ PreviousNext ❯
Download [Link]
The official [Link] website has installation instructions for
[Link]: [Link]
Getting Started
Once you have downloaded and installed [Link] on your computer, let's try to
display "Hello World" in a web browser.
Create a [Link] file named "[Link]", and add the following code:
[Link]
var http = require('http');
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]('Hello World!');
}).listen(8080);
Save the file on your computer: C:\Users\Your Name\[Link]
The code tells the computer to write "Hello World!" if anyone (e.g. a web
browser) tries to access your computer on port 8080.
For now, you do not have to understand the code. It will be explained later.
Command Line Interface
[Link] 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 "[Link]", the command line
interface window should look something like this:
C:\Users\Your Name>_
Initiate the [Link] File
The file you have just created must be initiated by [Link] before any action
can take place.
Start your command line interface, write node [Link] and hit enter:
Initiate "[Link]":
C:\Users\Your Name>node [Link]
Now, your computer works as a server!
If anyone tries to access your computer on port 8080, they will get a "Hello
World!" message in return!
Start your internet browser, and type in the address: [Link]