How to Initialize a Server on Node.js

How to Initialize a Server on Node.js

Table of contents

No heading

No headings in the article.

For Beginners, getting started with Node.js can be a daunting task. Here is a simple way how you can get started with Node.js and start a server for your app.

First, you have to download and install node.js on your machine. You can choose the versions according to the machine you are running. After installation, you can check to confirm it by opening up your command prompt and by typing node -v this will show the current version which means you have installed node.js successfully.

After successful Installation, create a project folder inside that directory fire up your code editor and in the terminal type: npm init -y . This will create an npm module and install all necessary dependencies for our app. you must also install express.js which is the framework to run javascript servers on node.js. Install it by typing in the terminal: npm i express .

after all the installations are done, you start writing the code for your server: the following code is how to start your server

//importing express 
const express = require('express');

//Creating an instance of the express object
const app = express();

app.listen(5000, console.log("server started on port 5000"));

To run your server code, you have to go to your terminal and type node and then the path of your server file eg: node backend/server.js . Here, the backend is the project folder on which our server is running

If everything is done right, Voila! your server will be up and running