DEV Community

Cover image for Getting started with NodeJS
Ali Zulfaqar for REKA

Posted on

Getting started with NodeJS

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.

  1. create a folder and run npm init with your CMD
npm init
Enter fullscreen mode Exit fullscreen mode

So what does npm init does is that it will initialize a package.json file

npm init command respond

  1. 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}/`);
});

Enter fullscreen mode Exit fullscreen mode
  1. Next, inside the folder directory use command
    node {yourwebserverfilename}

  2. web server code respond The image is showing that the web application is running on port 3001

  3. Open the browser and check whether it is giving a response or not.
    web server response
    what does the server response give

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 🙌🙌

Top comments (0)