5. node.js basics - SarthakJha/IECSE-Web-Summer-21 GitHub Wiki
APIs with node.js and express.js
For this week's task, you'll be required to create a simple API using a node.js framework called express
Before we get started with building an API though, it's important to understand just what an API means, how it works, and what's expected from a backend API server.
In simple words, API in terms of a web project is a way in which a backend server shares data with a client. The client can be the frontend of your website, or a python script, or any application which is authorized to use the service. It helps in modularising a project and makes it easy to implement changes in different parts of the project later.
Read up on the following topics before proceeding with the code:
- What's an API? Why are they so prominently used?
- What are REST APIs?
- What is HTTP?
- HTTP Methods for handling REST APIs
- HTTP Status Codes
- What's JSON?
Some video resources:
- What's an API?
- Setting up a REST API with node //you can ignore the mongoDB part for now
To use node.js and packages, you need to initialize a node project. To do that -
npm init
After this command is done, you'll find a file called package.json
, which contains information about your project, including the dependencies on other libraries.
To install express, simply run npm install express
this will add express into your package.json
as well as the node_modules
folder of your project.
After this is done, go through the express documentation to get started with building your API. You can use the startup code that is provided in the master branch to get started or start from scratch. Your API should serve the following requests:
- GET Request for all items.
- GET Request for a specific item (specified using a unique identifier).
- POST Request to create a new item.
- PUT Request to update a new item.