Introduction

What is node.js?

  • Node.js is an open-source server environment
  • Node.js is free
  • Node.js runs on various platforms
  • 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 and ASP handle 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 subsequent request

Here is how node.js handles a file request -:

  1. Sends the task to the computers file system
  2. Ready to take the subsequent request
  3. When the file system has opened and reads the file, the server returns the content to the client

What can Node.js do?

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

Getting started with Node.js

Installation

Install it from the official website of Node.js as per the instruction given

https://nodejs.org

Getting started

Let’s try to display “Hello World” in the web browser

Create a Node.js file and add the following code to it -:

var http = require('http');
var port = 5500; //your localport

http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type' : 'text/html'});
	res.end('Hello World');
}).listen(port);        

Save the code as myFirst.js in

“C:\Users\Your name\myFirst.js”

The above code tells the computer to write ‘Hello World’ if anyone tries to access your computer on port 5500.

Initiate the Node.js file

Node.js file must be initiated in the cmd program of your computer

Strat the cmd and navigate to the folder where you stored your myFirst.js file

then in the cmd write node myFirst.js

C:\Users\Your name>node myFirst.js


now your computer works as a server.

If anyone tries t access your computer on port 5500 they will get a “Hello World” message in return.

Start your browser and type in the address :

https://localhost:5500

要查看或添加评论,请登录

社区洞察

其他会员也浏览了