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.
For this series, I'm following an excellent video tutorial from Traversy Media
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