Nodejs Learning: The Hello World in Nodejs
Hello there, I will be on day to day basis for the next 100 days working to better you as we travel together to the world of Nodejs
This series assumes that you have Nodejs installed. If you haven't please do so and let us keep it rolling.
So, how do we write our first Node js Program?
Create a file anywhere and name it
app.js
.At the very top we have to require the `HTTP module
const http= require('http')
- Secondly we have to declare the
host
var host = '127.0.0.1' //localhost
- Now declare the port from which our app will run like so:
port = 3000
- The next step is to create server. To do that we use
CreateServer()
const server = http.createServer((req, res) => {
})
Inside createServer()
.We need to delare few things.
Status code
res.statusCode=200;
Content Type Header
res.setHeader('Content-Type','text/plain');
Now Send a simple Response
res.end('Getting started with nodejs');
6.Finally lets tell the server to listen to the host and port we created before
server.listen(port, hostname, () => {
console.log(`Holla the server is now running`);
});