Member-only story

How to setup a Node.js server port — Part 1

Aneeqa Khan
2 min readOct 12, 2022

What is Node.js?

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser, which was designed to build scalable network applications.

Today I want to show you a setup of the node server port so let's get into the implementation.

Install Node and npm

First, you have to download Node and npm from these links.

Get package.json file

Create a folder with your app name and create a file with server.js in that folder.
Now navigate into that folder and type the below command in the terminal.

npm init

It will ask you to provide the version, description, etc to type for your project. Just make sure to add server.js file for the entry point.

Install dependencies

Next, you have to install express which is the Node.js framework to create RESTful APIs and mongoose which is a JS library to create a connection between MongoDB and Node.js app.

npm i express mongoose

and install nodemon from this command

npm i -D nodemon

Nodemon is a tool to observe the changes in the file and restarts the server.

Write a script

Write these two scripts in your package.json file.

"scripts": {
"start": "node server.js",
"server": "nodemon server.js"
},

Create a port

Now, in the end, type this code in your server.js file

const express = require('express');
const port = 5000;
const app = express();app.listen(port, () => {
console.log(`Server started on port ${port}`);
});

and to get the results, run this command in terminal

npm run server

You’ll get the running port in the terminal just like this

Thank you for reading!
Feel free to connect on Twitter

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Aneeqa Khan
Aneeqa Khan

Written by Aneeqa Khan

I’m a frontend web and mobile developer specialized in web/mobile designs and frontend frameworks. I usually work with React, React Native, Next and TypeScript.

No responses yet

Write a response