Getting started with NodeJS
Ali Zulfaqar
Posted on December 30, 2021
Hello everyone wish you are well and good. For this post i will be sharing on how to get started with NodeJS backend. This is a really basic step. Lets get started
Getting the idea what is NodeJS
Node.js is a way for web applications to have real-time two-way connections, where both the client and server can initiate communication, allowing them to exchange data freely.
- create a folder and run npm init with your CMD
npm init
So what does npm init does is that it will initialize a package.json file
- For the next step, we need to create a file that contains code for the web application. For the naming of the file, it is upon your own naming convention. A lot of tutorial is using app.js, or server.js as the name of the file. Below is the code needed just to test whether our web server is running or not
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Next, inside the folder directory use command
node {yourwebserverfilename}
The image is showing that the web application is running on port 3001
Open the browser and check whether it is giving a response or not.
If you manage to reach the steps above basically you are done with the basic of NodeJS. There will be an upcoming post to continue from this post as this is just a basic on how to get started with NodeJS
Thank you for your time and i hope you can gain something from this post. See you in the next post 🙌🙌
Posted on December 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 20, 2024