I have created a Restful API that supports all HTTP methods and performs CRUD operations in a MongoDB database. The API is built using Express.js and connected to MongoDB using Mongoose. Additionally, the API includes basic server-side rendering capabilities.
- Node.js
- Express.js
- MongoDB
- Mongoose
The API supports the following CRUD operations:
- Create (POST): Create new records in the database.
- Read (GET): Retrieve data from the database.
- Update (PUT/PATCH): Modify existing records in the database.
- Delete (DELETE): Remove records from the database.
The API is connected to a MongoDB database using Mongoose, providing a robust and efficient way to interact with the data.
Express.js is used as the web application framework to handle routing, middleware, and HTTP requests/responses.
In addition to RESTful endpoints, the API supports basic server-side rendering. This feature enables the server to generate dynamic HTML content, enhancing the user experience.
To run the API locally, follow these steps:
- Install dependencies:
npm install
- Start the server:
npm start
- Access the API at
http://localhost:8000
- Endpoint:
/api/users
- Description: Create a new record in the database.
To add new records to the database using the API, you can use Postman – a popular tool for testing and developing APIs. Follow these steps to perform a POST request and create a new record:
-
Open Postman: If you don't have Postman installed, download and install it from here.
-
Set the Request Type: Choose the
POST
method from the dropdown menu. -
Enter the API URL: Set the URL to
http://localhost:8000/api/users
. -
Add Request Body:
- Select the
Body
tab in Postman. - Choose
raw
and set the format toJSON (application/json)
. - Provide the data you want to post in the request body. For example:
{ "firstName": "vicky", "lastName": "acharjee", "mail": "[email protected]", "jobTitle": "soft.dev", "gender": "Male" }
- Select the
-
Send the Request: Click the "Send" button to submit the POST request.
- Endpoint:
/api/users
- Description: Retrieve data from the database.
- Endpoint:
/api/users/:id
- Description: Modify an existing record in the database.
- Endpoint:
/api/users/:id
- Description: Remove a record from the database.
The API supports server-side rendering at the root URL (/users
). This feature enhances the user interface by rendering dynamic content on the server before sending it to the client.
Feel free to explore and extend this API for your specific needs!