Create Server
🎉
Read This on Github
This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?
⭐ Make a Basic Server?
Explain
-
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 (opens in a new tab).
- 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 (opens in a new tab).- 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 (opens in a new tab).
- 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.
- 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.
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 (opens in a new tab).
- 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
Normal Function
function multiply(a, b) {
return a * b;
}
console.log(multiply(2, 3));
Arrow Function
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 (opens in a new tab).