•  Home
  •  Dashboard
  •  Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
  •  Hosting Services
    • Infrastructure
      • iColocation
    • Compute
      • cMetal
      • cVirtual
    • Storage
      • sObject
      • sBlock
    • Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • Protection
      • pBackup
      • pDDoS
  •  Solutions
    • Ecommerce
    • Finance
    • Gaming
    • Hosting
    • Management
    • Security
    • System Integrator
  •  Support
    • Community
    • Knowledge Base
    • Open A Ticket
  •  USA & Canada: 800-933-1517
  •  International: 626-549-2801
  •  Email: sales@psychz.net
  • Services
    • new-colocation-header-img
      Infrastructure
      • iColocation
    • new-compute-header-img
      Compute
      • cMetal
      • cVirtual
    • new-storage-header-img
      Storage
      • sObject
      • sBlock
    • new-networking-header-img
      Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • new-protection-header-img
      Protection
      • pBackup
      • pDDoS
  • Solutions
    • Ecommerce
    • Security
    • Gaming
    • Hosting
    • Management
    • Finance
    • System Integrator
  • Dashboard

Node.js Web Server

  • Home
  • Client
  • Qa Forum
  • Node.js Web Server

Posted By: Mike | 1 Replies | Last Reply On: Aug 23, 2017 02:44:35

Can you guys help me on how to use Node.js as a web server? I would like to run a java based website on this technology. Also, if you can tell me if I should be using Node.js compared to Nginx or Apache?

Psychz - Girish

Votes: 0Posted On: Aug 23, 2017 02:44:35
 

Nodejs vs Apache vs Nginx

There is a lot of confusion among people about the difference between Nodejs and Apache and Nginx. I will try to throw light on some points which will help you in understanding their underlying concepts better. 

Node.js is an asynchronous process which implies that a single thread is used to run each node.js process. On the other hand, Apache, as well as Nginx, use multiple threads for multiple processes. Due to this, node.js can handle more number of requests as compared to Apache and is much faster when it comes to dynamic sites.

ALthough Nodejs can be used for dynamic as well as static sites, it's complete functionality is not justified in the case of static sites. It is more suitable to use Apache or Nginx in case of static sites. However, in the case of high-performance real-time applications, Nodejs can serve a great purpose.

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It means that Node.js provides a platform to run JavaScript on a server. Hence, we do not need server-based languages to develop a web application. You can use the Node.js application to create your own HTTP server. 

Here are the steps that will guide you towards creating your own HTTP server.

 

Installing Node.js

1.Please run the following command on your terminal to install the current version.

 

sudo apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs

 

2.You can also visit the Nde.js website and install the current version.

https://nodejs.org/en/

 

Create your own HTTP server

Create an empty file with the extension ".js". We are creating a file called "Hello.js" for our example. Use the following command for the following.

 

sudo touch Hello.js

 

Once the file is created, edit the file and enter the following code.

 

var http=require('http')
var server=http.createServer(function (request, response)
{
response.writeHead(200, {'Content-Type' : 'text/plain'});
response.end("You have entered\n");
});
server.listen(80);

 

The functionality of this code is that it reads the JavaScript, executes it and returns the object. Let us guide you through the code line by line.

1. We include the HTTP module to get the required functions from it so that they can be used in the application.

2. We create a server application with the functions of "request and response". The "request" function is called every time the user requests for a URL. The "Response" function is the response we give to the request.

3. When a request is received, we are saying to send a response with a header type of '200.' This number is the normal response which is sent in an http header when a successful response is sent to the client. The "content type" is text. Hence, the response should be in text format.

4. This is the response that will be given to the request. It will display "You have entered" on the screen and end the response.

5. The server will listen to Port 80 which is the default HTTP port. However, it is not mandatory.

 

Final Step

After you have finished writing the code, type the following command to get your HTTP server running.

 

node Hello.js

 

You can check the working of HTTP server by going to your browser. When you type in "localhost", the message "You have entered" will be displayed. 

Was this reply helpful?

Related topics

  • Best Web Server for Node.js
  • How To Run Kubernetes on Psychz Networks Bare Metal Servers
  • Nodejs https server
  • What are different types of web hosting servers?
  • Spain Dedicated Server
  • Dedicated Server for VPS Node
  • CDN and Load Balancer: Ultimate combination for efficient content delivery
  • How to install Node.js on AlmaLinux 8
  • Best 10Gbps Unmetered Dedicated Servers - Top 10 List
  • Learn About Anycast And Some Of Its Advantages And Disadvantages
  • sBlock - iSCSI Linux Guide
  • Best Dedicated Server Providers In Dallas: A Comprehensive Guide
  • Best Dedicated Server Providers in Los Angeles: Top 10 List
  • Why Gaming Companies are Choosing Bare Metal Servers
  • Managing systemd services and units using systemctl commands
Copyright © 2025 Psychz Networks,
A Profuse Solutions Inc Company
Hosting Services
Infrastructure
  • iColocation
Compute
  • cMetal
  • cVirtual
Storage
  • sObject
  • sBlock
Networking
  • nCDN
  • nIP Transit
  • nWavelength
Protection
  • pBackup
  • pDDoS
Company
  • About Us
  • Blog
  • Careers
  • Contact Us
  • Data Centers
  • Looking Glass
  • Network
  • Reseller
Policies
  • Acceptable Usage Policy
  • Privacy Policy
  • Service Level Agreement
  • Terms and Conditions
Support
  • Community
  • Knowledge Base
  • Open A Ticket
Get In Touch
  • Psychz Networks,
    A Profuse Solutions Company
    611 Wilshire Blvd #300
    Los Angeles,California 90017
    USA
  • US/Canada: 800-933-1517
  • International: 626-549-2801