Node js
Node.js is an open source server framework.
Node.js allows you to run JavaScript on the server.
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:
Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.
create a sever
Save the file on your computer: C:\Users.............
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.
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:
Start your command line interface, write
by using this code for creating a module
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Use the
The function passed into the
Save the code above in a file called "demo_http.js", and initiate the file:
Node.js allows you to run JavaScript on the server.
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.
- 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 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"
create a sever
Save the file on your computer: C:\Users.............
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
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:
Initiate the Node.js File
The file you have just created must be initiated by Node.js before any action can take place.Start your command line interface, write
node myfirst.js
and hit enter:by using this code for creating a module
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
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:Example
var http = require('http');
//create a server object:
http.createServer(function
(req, res) {
res.write('Hello World!'); //write a response to the
client
res.end(); //end the response
}).listen(8080); //the
server object listens on port 8080
The function passed into the
http.createServer()
method, will be executed when someone tries to access the
computer on port 8080.Save the code above in a file called "demo_http.js", and initiate the file:
Comments