A simple Node.js REST API for user management using Express.js and SQLite database.
This project is intended for learning and demonstration purposes only. The folder structure and overall architecture are not optimized for production use, scalability, or long-term maintainability.
- Full CRUD operations for users
- SQLite database with automatic table creation
- Automatic avatar generation using Avatar Placeholder API
- Sample data insertion with avatars
- Gender-based avatar assignment
- JSON responses
- Error handling
- CORS enabled
- Environment configuration
This API integrates with Avatar Placeholder to automatically generate diverse avatars for users:
- Gender-based avatars: Male and female specific avatars
- Random generation: Each user gets a unique random avatar
- Automatic assignment: Avatars are generated on user creation
- Avatar refresh: Update avatar endpoint to generate new avatars
project/
├── src/
│ ├── server.js # Entry point
│ ├── db.js
│ └── routes.js
├── data/
│ └── users.db
├── .gitignore
├── package.json
├── readme.md
- Clone / Fork the project :
npm install express sqlite3 cors
npm install -D nodemon
- Create .env file in the project root:
DB_FILE=data/users.db
PORT=3000
- Start the server:
# Development mode with auto-reload
npm run dev
# Production mode
npm start
The server will start on http://localhost:3000
Get all users
curl http://localhost:3000/api/users
Get a specific user by ID
curl http://localhost:3000/api/users/1
Create a new user
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice Brown", "email": "alice@example.com", "age": 28}'
Update an existing user
curl -X PUT http://localhost:3000/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "John Updated", "email": "john.updated@example.com", "age": 31}'
Delete a user
curl -X DELETE http://localhost:3000/api/users/1
Health check endpoint
curl http://localhost:3000/api/health
The users table has the following structure:
id
: INTEGER PRIMARY KEY (auto-increment)name
: TEXT NOT NULLemail
: TEXT UNIQUE NOT NULLage
: INTEGERcreated_at
: DATETIME (default: current timestamp)
{
"message": "Users retrieved successfully",
"data": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"created_at": "2025-01-15 10:30:00"
}
]
}
project/
├── server.js # Main server file
├── package.json # Dependencies and scripts
├── users.db # SQLite database (created automatically)
└── README.md # Documentation
The API includes proper error handling for:
- Invalid requests (400)
- Resource not found (404)
- Duplicate email addresses (409)
- Server errors (500)