ExpressJS is a widely used framework for creating RESTful APIs in NodeJS. Creating an Express server is pretty much easy. We just need to install some NPM package to create a server. I am assuming you have Visual Studio Code installed in your computer. So let’s start!
N.B. You have to install NodeJS in your system first.
Step – 1:
Create a folder in your directory. Open VS Code terminal and navigate to that directory. Run the following command to initiate NPM in your project.
$ npm init -y
This will create a package.json file in the directory.
Now create a folder named src/source or whatever you want to name it. In this folder we will create our server/main file. Name your server file as server.js/index.server.js or anything you want. Now edit your main and start script and add the following line as picture –
Step – 2:
We are done with our package.json file. Now go to the terminal and install the following NPM packages –
$ npm install express nodemon
Here, Nodemon is a simple tool that helps to restart your server when you make changes to any of the files. After successful installation, write your index.server.js file accordingly.
Now go to the terminal and run npm start. Your server will start working.
If you want to check into the browser just add the below 2 lines of code to create a root route.
Go to the browser, type http://localhost:3000, hit enter. You will get the message from server.
Your basic express server is ready for further development. Now you can create multiple routes and controllers inside the src folder.