This project demonstrates how to create a simple Node.js + Express server that interacts with an external API β in this case, JSONPlaceholder. It's designed to help beginners understand:
- Setting up an Express server
- Using the
fetch
API in Node.js (vianode-fetch
) - Making HTTP requests (GET, POST, PUT, PATCH, DELETE)
- Applying a basic MVC structure using controllers and routers
- Express-based backend
- Proxies requests to JSONPlaceholder
- Full CRUD support for
/posts
- Simple MVC folder structure (in the
express-with-mvc
branch)
main
: Flat, single-file versionexpress-with-mvc
: MVC-style organization usingapp.js
, routers, and controllers
- Node.js 18+ (for ESM + fetch support or use
node-fetch@2
) - NPM
# Clone the repo
git clone https://github.com/hackthegap/sample-node-express-fetch.git
cd sample-node-express-fetch
# Install dependencies
npm install
# Start the server
npm start
The server will be running at:
http://localhost:3000
Method | Endpoint | Description |
---|---|---|
GET | /posts |
List all posts |
GET | /posts/:id |
Get a single post |
GET | /posts/:id/comments |
Get comments for a post |
POST | /posts |
Create a new post |
PUT | /posts/:id |
Fully update a post |
PATCH | /posts/:id |
Partially update a post |
DELETE | /posts/:id |
Delete a post |
Fabricio Braga
GitHub: hackthegap
Last updated: 2 Apr 2025
Let me know if youβd like me to generate a version for the `main` branch too or add badges/shields (like Node version, etc.)!