Node.js : Create back-end server in Node.js in less than 5 minutes
Tags: node, webdev, backend
Published: 2021-05-08 07:12:41

# 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.

```javascript
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
```

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:
```javascript
node hello.js
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhyzwxpqkljxdhexxas2.png)


# 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. 
```javascript
// 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);
});
```
From the command line, run the following command:
```javascript
node hello.js
```
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.
{% stackblitz node-p5ftqk %}

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](https://www.linkedin.com/in/www-rajeshkumaryadav-com/) and [Twitter](https://twitter.com/rky_com).

<a href="https://www.buymeacoffee.com/rkycom" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>