DEV Community

Cover image for Node.js : Create back-end server in Node.js in less than 5 minutes
RajeshKumarYadav.com
RajeshKumarYadav.com

Posted on • Updated on

Node.js : Create back-end server in Node.js in less than 5 minutes

Hello World HTTP server with Core Module

First, install Node.js for your platform.

In this example we'll create an HTTP server listening on port 1337, which sends Hello, World! to the browser. Note that, instead of using port 1337, you can use any port number of your choice which is currently not in use by any other service.

The http module is a Node.js core module (a module included in Node.js's source, that does not require installing
additional resources). The http module provides the functionality to create an HTTP server using the http.createServer() method.

To create the application, create a file containing the following JavaScript code.

const http = require('http'); // Loads the http module
http.createServer((request, response) => {
 // 1. Tell the browser everything is OK (Status code 200), and the data is in plain text
 response.writeHead(200, {
 'Content-Type': 'text/plain'
 });
 // 2. Write the announced text to the body of the page
 response.write('Hello, World!\n');
 // 3. Tell the server that all of the response headers and body have been sent
 response.end();
}).listen(1337); // 4. Tells the server what port to be on
Enter fullscreen mode Exit fullscreen mode

Save the file with any file name. In this case, if we name it hello.js we can run the application by going to the
directory the file is in and using the following command:

node hello.js
Enter fullscreen mode Exit fullscreen mode

The created server can then be accessed with the URL http://localhost:1337 or http://127.0.0.1:1337 in the browser.

A simple web page will appear with a β€œHello, World!” text at the top, as shown in the screenshot below.
Alt Text

Hello World HTTP server with Express

The following example uses Express to create an HTTP server listening on port 3000, which responds with "Hello,
World!".

Express is a commonly-used web framework that is useful for creating HTTP APIs.

First, create a new folder, e.g. myApp. Go into myApp and make a new JavaScript file containing the following code
(let's name it hello.js for example). Then install the express module using npm install --save express from the command line.

// Import the top-level function of express
const express = require('express');
// Creates an Express application using the top-level function
const app = express();
// Define port number as 3000
const port = 3000;
// Routes HTTP GET requests to the specified path "/" with the specified callback function
app.get('/', function(request, response) {
 response.send('Hello, World!');
});
// Make the app listen on port 3000
app.listen(port, function() {
 console.log('Server listening on http://localhost:' + port);
});
Enter fullscreen mode Exit fullscreen mode

From the command line, run the following command:

node hello.js
Enter fullscreen mode Exit fullscreen mode

Open your browser and navigate to http://localhost:3000 or http://127.0.0.1:3000 to see the response.

For more information about express you can visit https://expressjs.com/

Demo

Run node index.js command in terminal and then server will get started.

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.

Buy Me A Coffee

Top comments (0)