Even making a simple server and adding routes takes a large number of code lines, and code readability decreases. Express, a Node.js framework, solves this problem by providing a much faster way to create the output.
One great thing about Express is that it’s unopinionated, which means it doesn’t hold on to a specific file structure. This feature has its own pros and cons. There are also opinionated options in the market, like NestJS.
Installation
Add a package.json file → → install express
npm init -y
npm i express
if you want you can add nodemon → its a dev dependecy and makes developement easy → we dont have to manually restart server again and again.
npm i nodemon
Some Changes I generally make…
step-1 : create .gitignore file and a src directory.
.gitignore
specifies the files and folders we don't want to push to GitHub. It's a good practice not to push the node_modules
folder, as it contains all the installed dependencies, which can be easily recreated using npm install
→package.json
is an essential file that defines the project's metadata and dependencies. package-lock.json
locks the exact versions of the dependencies and their sub-dependencies, ensuring consistency across different environments. It is an autogenerated file and should always be committed along with package.json
to maintain dependency integrity.step-2 : add index.js or index.mjs file → add Scripts in package.json file
I preffer working with ES6 JS so we have to make .js → .mjs OR add “type“:”module” in package.json
made an index.js file and added scripts to run index.js with node and nodemon.
Creating a simple server
Running that server using nodemon via scripts.
PORT
variable. Additionally, you should use process.env.PORT
to make your application compatible with deployment environments. If process.env.PORT
is not included, the code may work on localhost but fail during deployment.we have not added any routes hence shows this output.
Route Parameters
In request like localhost:3000/api/username → we often require the specific object to be returned back which follows our desired parameters.Here we use request parameters.
this is done by req.params
example →
→ Here, we first have to specify the dynamic segment using :
before the parameter.
→ I want to get the user of a specific ID, so I created a route with the dynamic segment :id
. To get that key-value pair, we use req.params.id
.
→ Then, parse this id
into an integer using parseInt
.
→ If it’s not an integer, send a status code of 400
; else, use .find
and a callback function to get the object with the same id
.
→ If found, return it, or if not, a status code of 404
is sent.
Query Parameters
Query parameters are often used to send extra data at the endpoints.Typically used to filter,sort or pass additional data.They are part of the URL and come after a ?
symbol. Multiple query parameters are separated by &
.
this is for the request given as http://localhost:3000/api/users?filter=lastName&value=a
here query parameters are used to filter the objects in api.
Conclusion →
this is a brief intro on express and some basic routing with creating a simple api.
next → different HTTP methods and Middleware.
edit : also planning to add a project using fs module. coming soon