Hands-on Practice 1
Step-by-Step Coding Procedure:
Setup and Initialize Project:
Create a project folder
express-crud-app
.Initialize Node project and install Express:
Create Basic Server (
index.js
):Set up basic Express server index.js for Managing User
Implement CRUD Operations:
Create RESTful API routes for managing
users
as shown above.RESTful API Routes for Managing Users:
4. Testing with Postman:
GET: Retrieve All Users
Open Postman.
Select GET from the dropdown.
Enter
http://localhost:3000/users
in the URL field.Click Send.
Expected Result: A list of all users in JSON format.
POST: Add a New User
Select POST from the dropdown.
Enter
http://localhost:3000/users
in the URL field.Go to the Body tab, select raw, and choose JSON format from the dropdown.
Enter the following JSON data:
Click Send.
Expected Result: The new user object is returned with an assigned ID.
PUT: Update User Details
Select PUT from the dropdown.
Enter
http://localhost:3000/users/1
(replace1
with the user ID you want to update).Go to the Body tab, select raw, and choose JSON format.
Enter the updated JSON data:
Click Send.
Expected Result: The updated user object is returned.
DELETE: Remove a User
Select DELETE from the dropdown.
Enter
http://localhost:3000/users/1
(replace1
with the user ID you want to delete).Click Send.
Expected Result: No content returned (status code 204), indicating the user was successfully deleted.
By following these steps, you'll be able to effectively test your RESTful API endpoints using Postman.
Student Activity:
Build a RESTful API to manage a "tasks" list (similar CRUD operations as above).
Test all API endpoints using Postman.
Bonus: Add validation to ensure all fields are correctly provided before adding or updating a user.
By the end of this session, students will understand key Node.js and Express concepts, be able to build web applications, and create RESTful APIs with real-world examples.
Last updated