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..
Create a file named
server.js
in the root directory of your project.Add the following code to the file.
const http = require('http');
This will handle the server's request and response.
- Create a server using createServer() method.
http.createServer().listen(4500);
This method can take a function as a parameter. We use this
createServer()
method to create a server. Thelisten()
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.
- Now, You have to pass two functions
req
andres
as a parameter to thecreateServer()
method.
http.createServer((req, res) => {
}).listen(4500);
- 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 theres
parameter is used to send the response to the client.
- 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);
- 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 useend()
method to end the response. You can check this on official documentation of http.
- Now, run the server using the following command.
node server.js
- Now, open the browser and type
localhost:4500
in the address bar. You will see the following output.
Hello World
- You can also send a html tag to the client.
http.createServer((req, res) => {
res.write('<h1>Hello World</h1>');
res.end();
}).listen(4500);
- But remember you have to run the server again.
Let's break down the code .
- FOR EXAMPLE: We will create a function named
data()
and we will pass thereq
andres
as a parameter to the function.
const data = (req, res) => {
res.write('Hello World');
res.end();
}
or
function data(req, res) {
res.write('Hello World');
res.end();
}
- Now, we will pass the
data
function as a parameter to thecreateServer()
method.
http.createServer(data).listen(4500);
- Now run the server using the following command.
node server.js
- 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.
- We just copy the
data()
function and paste it in thecreateServer()
method.
old
http.createServer(
// function body
).listen(4500);
new
http.createServer(
// function body
(req, res) => {
res.write('Hello World');
res.end();
}
).listen(4500);
📌 Simplified Code
function multiply(a, b) {
return a * b;
}
console.log(multiply(2, 3));
const multiply = (a, b) => a * b;
console.log(multiply(2, 3));
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)