Node.js RESTful API
๐ Node.js RESTful API (Complete Guide)
A RESTful API is an API that follows REST principles and allows clients to interact with a server using HTTP methods:
| Method | Purpose |
|---|---|
| GET | Read data |
| POST | Create data |
| PUT | Update data |
| DELETE | Remove data |
We will build a simple REST API using Node.js + Express.js.
๐ฆ 1. Project Setup
Install Express:
๐๏ธ 2. Create Basic Server
Create server.js:
Run the server:
๐๏ธ 3. Sample Data (Fake Database)
We’ll store data temporarily in an array.
๐งฉ 4. REST API Endpoints
โ GET All Users
โ GET Single User by ID
โ POST Create New User
โ PUT Update User
โ DELETE Remove User
๐ Full Express REST API Example
Here is the full server.js:
๐งช Testing the API
You can test it using:
โ Postman
โ Thunder Client (VS Code)
โ curl
Example curl test:
๐ Bonus: Add CORS Support
โ๏ธ Bonus: Use Nodemon (Auto Restart)
Add to package.json:
Run:
