This repository provides a minimal Express.js API with JWT-based authentication. It uses SQLite via Sequelize for data persistence and demonstrates typical CRUD operations. The example is suitable for integration with a React.js frontend.
- Install dependencies:
cd server
npm install- Start the server:
npm startThe server listens on port 3000 by default and stores data in database.sqlite.
When NODE_ENV=production, Sequelize connects using environment variables instead of SQLite.
Set the following variables to configure a PostgreSQL or MySQL database:
DB_DIALECT–postgresormysqlDB_HOST– database hostDB_PORT– database portDB_NAME– database nameDB_USER– database userDB_PASS– database password
Create a new user and receive a JWT token.
Body:
{ "username": "alice", "password": "secret" }Authenticate an existing user and receive a JWT token.
Body:
{ "username": "alice", "password": "secret" }Tokens expire after one hour. Include the token in the Authorization header as Bearer TOKEN when accessing protected routes.
All /api/items routes require authentication.
GET /api/items– list items for the current userPOST /api/items– create a new itemPUT /api/items/:id– update an itemDELETE /api/items/:id– delete an item
Example request with curl:
curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:3000/api/itemsA React component can interact with the API using fetch or axios:
// using fetch
async function loadItems(token) {
const res = await fetch('http://localhost:3000/api/items', {
headers: { Authorization: `Bearer ${token}` }
});
const data = await res.json();
console.log(data);
}- Register a user using
POST /register. - Copy the returned token.
- Set the
Authorizationheader toBearer TOKENin subsequent requests. - Use
GET /api/itemsor other endpoints to verify authenticated access.
This project is intentionally simple to serve as a starting point for building full-stack applications.