Skip to content

Repository files navigation

🔌 REST API - Node.js + Express + Sequelize

A production-style RESTful API for a small blogging domain (Users, Posts, JWT auth), built with Express and Sequelize. It runs on SQLite out of the box (no database to install), ships with automated tests, Swagger docs, and a Dockerfile, and swaps to Postgres/MariaDB with one environment variable.

CI tests node


✨ Highlights

  • 🔐 JWT authentication - register / login, Bearer-token protected routes
  • 🧂 Password hashing with bcrypt (hashes are never returned in responses)
  • 👤 Ownership authorization - you can only edit/delete your own posts (403 otherwise)
  • 🔗 Relations - User hasMany Post; responses embed the author
  • 📄 Pagination + search - GET /posts?page=&limit=&tag=&q=
  • ✅ Automated tests - Jest + Supertest against an in-memory database (9 tests)
  • 📚 Interactive API docs - Swagger UI at /docs
  • 🐳 Dockerized - docker compose up and you're running
  • 🧯 Centralized error handling, rate limiting

🚀 Quick start

npm install
npm run seed     # optional sample data (login: ada@example.com / secret1)
npm start        # http://localhost:3000  · docs at /docs

Or with Docker:

docker compose up --build

Run the tests:

npm test

🔑 Auth flow

# 1. Register (returns a JWT)
curl -X POST localhost:3000/auth/register -H 'Content-Type: application/json' \
  -d '{"firstname":"Aziz","lastname":"BENAYED","email":"aziz@example.com","password":"s3cret"}'

# 2. Use the token to create a post
curl -X POST localhost:3000/posts -H "Authorization: Bearer <TOKEN>" \
  -H 'Content-Type: application/json' \
  -d '{"title":"Hello","content":"World","tags":"intro"}'

📚 Endpoints

Method Path Auth Description
POST /auth/register - Create account, returns JWT
POST /auth/login - Log in, returns JWT
GET /users - List users
GET /users/me ✅ Current user + their posts
GET /users/:id - A user with their posts
GET /posts - Paginated list (?page,?limit,?tag,?q)
POST /posts ✅ Create a post (author = you)
GET /posts/:id - One post with author
PUT /posts/:id ✅ Update your post
DELETE /posts/:id ✅ Delete your post

⚙️ Configuration

Env var Default Purpose
PORT 3000 HTTP port
JWT_SECRET dev-secret-change-me Token signing secret (set in production)
DB_STORAGE database.sqlite SQLite file path
DATABASE_URL - Full DB URL (Postgres/MariaDB) to replace SQLite

🏗️ Project structure

src/
  app.js            Express app (routes, swagger, error handling)
  db.js             Sequelize connection (sqlite / memory / DATABASE_URL)
  models/index.js   User & Post models, hooks, associations
  middleware/auth.js JWT sign + authenticate
  routes/           auth.js · users.js · posts.js
  openapi.js        Swagger spec
tests/api.test.js   Jest + Supertest suite

🛠️ Tech Stack

Express Sequelize JWT SQLite Jest Swagger Docker


📄 License

Released under the MIT License.

About

RESTful CRUD API (Users & Posts) with Express + Sequelize, runs on SQLite out of the box

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages