Getting started with express.js in 2 min
Express.js is a nodejs framework basically used to create a backend server for hosting APIs and can also be used for hosting files or serving them. In this blog, I will be telling you how to get started with express in a minimum amount of time and run your server and start hosting APIs. Without wasting any time, Let’s get started.
Installation
First of all, create a package.json file by running
npm init -y
This will create a package.json file. Add an extra attribute of “type”:”module”
Your package.json file will look something like this
Now let’s install expressjs
npm install express
Let’s create and run our server using express installed.
Before this, let’s install some more dependencies that will help us:
npm install nodemon --save-dev //for running server in development
npm install cors //for handling cors
npm install body-parser //for taking input
Create a server.js file and write the following code in it:
Setup nodemon to start server.js file in development mode by adding scripts in package.json file like this:
"scripts": { "start":"node server.js", "dev":"nodemon server.js"}
package.json will look something like this:
Run npm run dev
This will run the server again everytime you change anything in the code. Go to http://localhost:3000/hello, you will see the output as {“hello”:”world”}. Make as many routes you want now. That’s it. Enjoy making APIs and ship products in minimum time.
I hope you like the blog and if you do, don’t forget to hit the clap icon and follow me for more such blogs.