Skip to content

DevilsDev/API-Playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

API Playground

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.

Setup

  1. Install dependencies:
cd server
npm install
  1. Start the server:
npm start

The server listens on port 3000 by default and stores data in database.sqlite.

Production database configuration

When NODE_ENV=production, Sequelize connects using environment variables instead of SQLite. Set the following variables to configure a PostgreSQL or MySQL database:

  • DB_DIALECTpostgres or mysql
  • DB_HOST – database host
  • DB_PORT – database port
  • DB_NAME – database name
  • DB_USER – database user
  • DB_PASS – database password

Authentication Endpoints

POST /register

Create a new user and receive a JWT token.

Body:

{ "username": "alice", "password": "secret" }

POST /login

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.

Item API

All /api/items routes require authentication.

  • GET /api/items – list items for the current user
  • POST /api/items – create a new item
  • PUT /api/items/:id – update an item
  • DELETE /api/items/:id – delete an item

Example request with curl:

curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:3000/api/items

React Integration Example

A 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);
}

Testing with Postman or Curl

  1. Register a user using POST /register.
  2. Copy the returned token.
  3. Set the Authorization header to Bearer TOKEN in subsequent requests.
  4. Use GET /api/items or other endpoints to verify authenticated access.

This project is intentionally simple to serve as a starting point for building full-stack applications.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors