Hi Guys,




In this Tutorial we will see how we can create a simple node server.
There Can be two different type of node servers:
So lets begin with the task in hand......
Lets start with the prerequisites :
- Node
- Editor
- internet connection
lets create a simple file name as server.js
fist thing we needs to do is requiring the required node modules such as http
const http = require("http");
After importing the module we need to perform two tasks
- Creating a server
- listen the requests which came to this server.
Lets start with creating with server
Creation of a server is a simple task in hand with the following command.
const server = http.createServer();
now you have a variable server which point to our created server.
but at this point of time you haven't defined how it will get the response.
let create a simple listener as well by using below piece of code.
server.listen(5000, "127.0.0.1", () => {
console.log("Server started");
});
now you have created a listener as well. but we haven't started our server yet . If we are working with node we can simply start our server with the help of command
node server.js
As you can see in the above screen shot you got the message as server is started. now You server has been started but you will not be able to get any response when you try to hit this with your browser.
So let change our server config such that we can get the results.
const server = http.createServer((req, res) => {
console.log("you got a request");
res.end("You got the response");
});
i have modified the above code such that we can hit our server from the browser and get the result as well like You got the response.
Now our complete server.js looks like this :
const http = require("http");
const server = http.createServer((req, res) => {
console.log("you got a request");
res.end("You got the response");
});
server.listen(5000, "127.0.0.1", () => {
console.log("Server started");
});
now you just need to start the server using node server.js and then hit the browser with your localhost with port 5000.
So now we are done with creating a simple http node server but it doesn't seems like you have some correct data so let modify our code some more such that we can get some better output with some more details.
lets send the status code as well some header details because sometime it happens you will face situation where your browser is not able to understand what you have send from the back end .
let try to send the status code 200 first.
so can you a node inbuilt function writeHead(). there you can specify the header details status code and much more.
res.writeHead(200, {
'content-type': 'text/html'
});
here first argument specifies the status code and the second element specifies the header details, you can pass multiple header details at the same time such as cors , content type ,encryption,location etc
Lets try to add this peice of code to server.js
now our server.js looks like below
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, {
"content-type": "text/html"
});
res.end("You got the response with correct status");
});
server.listen(5000, "127.0.0.1", () => {
console.log("Server started");
});
let hit the browser as well.
Please comment if you have suggestion or any confusion.
Comments
Post a comment