website development company
day February 17, 2022 comm 0 Comments

How To Build a Website Development Server from Scratch with Node.js?

Not everyone wants a website on a CMS platform. Some of us want our own website development server built from scratch. For that, you need a good website development company in UK to build a server using node.js.

Building a backend using Node.js doesn’t take that long. If you know programming, then working with Node.js with the help of our guide will be easy. Let’s understand from the beginning.

 

What is Node.js?

Node.js is a JavaScript runtime environment. It easily builds fast as well as scalable applications. It runs the JavaScript code on the server-side and hence, produces dynamic web content. Various modules are present in Node.js, for instance, HTTP and request. 

How Do Servers Work?

Do you know what a server is? When you search for anything on the net, the request goes to another computer which gives your required webpage as the output. That computer is called a web server. The web server, thus, receives HTTP requests from clients and accordingly, produces an HTTP response.

Node.js helps us to write this server code. Thus, over time. Node.js has become the most popular choice for developers to write backend code.

How To Install Node.js?

First, you need to install the Node.js framework. Here’s how you go about it:

  1.  Download Node.js Installer for your Windows OS.
  2. Run the installation.
  3. Start the installation.
  4. Test the following command in your Command Prompt to check the updated version: $ node -v.

 

Build The Server Using Node.js

We are using Visual Studio Code for our project. Here’s how you go about it:

  • Create the project folder using the required command:

Create your project folder using the following command.

mkdir node-server-tutorial

  • Inside the folder, make a file and name it server.js

Use the HTTP module to write the server.js file.

// server.js

var http = require(‘http’);

var server = http.createServer(function(req, res) {

    res.writeHead(200, { “Content-type”: “text/plain” });

    res.end(“Hello world\n”);

});

server.listen(3000, function() {server.js file, 

    console.log(‘Server is running at 3000’)

});

  • “Require http” code needs you to have the HTTP module in your file.
  • We have created the server using the method createServer.
  • The method takes request and response parameters.
  • A response is sent to the client.
  • The application runs at port 3000.
  • Run the Node.js server

Now that you have written the node.js file, it is time to run the Node.js server. In the terminal, write ‘node server’. Now, open your preferred browser and go to http://localhost:3000 since our application is running at port 3000. Hurray! A plain text shows Hello World, the response we had sent in our server.js file.

  • Now, install the express framework

  • Go to your terminal and write ‘npm init’.
  • Now install the express framework.

 npm install express –save

  • Now we will write the code using the express module and two commands: get() and listen()

// server.js

var express = require(‘express’);

var app = express();

var PORT = 3000;

app.get(‘/’, function(req, res) {

    res.status(200).send(‘Hello world’);

});

app.listen(PORT, function() {

    console.log(‘Server is running on PORT:’,PORT);

});

After completing the above steps, you start the server again. Finally, your Node.js server is complete.

Also Read : 

TOP WORDPRESS THEME & PLUGIN DETECTOR 2022

 

0 Comments

Comments are closed.