DEV Community

Cover image for Getting Started with Node.js: Building Your First Server
Subham
Subham

Posted on • Updated on

Getting Started with Node.js: Building Your First Server

Hi, I'm Subham Maity, a software engineer. I also enjoy teaching others how to code through my tutorials. I'm always eager to learn new things and share my knowledge with the community.

⚡ I recently wrote an article on Getting Started with Node.js: Building Your First Server and wanted to share it with you all. You can find the article on my website https://codexam.vercel.app/docs/node/node1 [Better View]

⚡ I also have a repository on GitHub where you can find all the code and projects related to this topic. You can find the repository at https://github.com/Subham-Maity/node-js-full-stack-tutorial

❗ For a more in-depth look at this topic, including a detailed table of contents, check out the complete tutorial on my GitHub Repo

If you want to stay updated with my latest projects and articles, you can follow me on:

Let's make a basic server..

  1. Create a file named server.js in the root directory of your project.

  2. Add the following code to the file.

const http = require('http');
Enter fullscreen mode Exit fullscreen mode

This will handle the server's request and response.

  1. Create a server using createServer() method.
http.createServer().listen(4500);
Enter fullscreen mode Exit fullscreen mode
  • This method can take a function as a parameter. We use this createServer() method to create a server. The listen() method is used to listen to the port number means the server will start on the port number 4500.

  • You can check this on official documentation of http.

  1. Now, You have to pass two functions req and res as a parameter to the createServer() method.
http.createServer((req, res) => {

}).listen(4500);
Enter fullscreen mode Exit fullscreen mode
  • This is arrow function and the syntax of the arrow function is abc((req, res) => {}). First bracket is for the parameter and the second bracket is for the function. You can check this on official documentation of arrow function.
  • The req parameter is used to get the request from the client and the res parameter is used to send the response to the client.
  1. I assume that no one actually sends a request to the server. So, we will send a response to the client.

http.createServer((req, res) => {
    res.write('Hello World');
    res.end();
}).listen(4500);
Enter fullscreen mode Exit fullscreen mode
  • We need to use write() method to send the response to the client. The write() method can take a string or a buffer as a parameter. We can also use end() method to end the response. You can check this on official documentation of http.
  1. Now, run the server using the following command.
node server.js
Enter fullscreen mode Exit fullscreen mode
  1. Now, open the browser and type localhost:4500 in the address bar. You will see the following output.
Hello World
Enter fullscreen mode Exit fullscreen mode
  1. You can also send a html tag to the client.
http.createServer((req, res) => {
    res.write('<h1>Hello World</h1>');
    res.end();
}).listen(4500);
Enter fullscreen mode Exit fullscreen mode
  • But remember you have to run the server again.

Let's break down the code .

  1. FOR EXAMPLE: We will create a function named data() and we will pass the req and res as a parameter to the function.
const data = (req, res) => {
    res.write('Hello World');
    res.end();
}

Enter fullscreen mode Exit fullscreen mode

or

function data(req, res) {
    res.write('Hello World');
    res.end();
}
Enter fullscreen mode Exit fullscreen mode
  1. Now, we will pass the data function as a parameter to the createServer() method.
http.createServer(data).listen(4500);
Enter fullscreen mode Exit fullscreen mode
  1. Now run the server using the following command.
node server.js
Enter fullscreen mode Exit fullscreen mode
  • You will get the same output as the previous one.

Let's Understand.

Basically, arrow function is a short form of the function just use const function_name = (parameter) => {function body} instead of function function_name(parameter) {function body}. You can check this on official documentation of arrow function.

  1. We just copy the data() function and paste it in the createServer() method.

old

http.createServer(
// function body
).listen(4500);
Enter fullscreen mode Exit fullscreen mode

new

http.createServer(

// function body
(req, res) => {
    res.write('Hello World');
    res.end();
}

).listen(4500);
Enter fullscreen mode Exit fullscreen mode

📌 Simplified Code

function multiply(a, b) {
    return a * b;
}
console.log(multiply(2, 3));
Enter fullscreen mode Exit fullscreen mode
const multiply = (a, b) => a * b;
console.log(multiply(2, 3));
Enter fullscreen mode Exit fullscreen mode

here return is not required because it is a single line function.

Additionally you can check the js tutorial which is available on my website. You can check this on official documentation of arrow function.

Top comments (0)