This project demonstrates a Node.js API using the built-in Node.js SQLite module (DatabaseSync) with a MVC structure, similar to a typical MERN stack project. It includes basic CRUD operations for a users table.
node-sqlite-crash/
├── package.json
├── server.js # Express server entry point
├── db/
│ └── database.js # SQLite database connection and schema creation
├── models/
│ └── userModel.js # Model layer: SQL queries / mini ORM
├── controllers/
│ └── userController.js # Controller layer: Handles HTTP requests
└── routes/
└── userRoutes.js # Route layer: Maps endpoints to controllers
- Uses Node built-in
DatabaseSyncto create a SQLite database file (data.db) and auserstable. - Example schema:
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
)- The
database.jsfile ensures the table exists when the server starts.
-
Functions (or classes) to interact with the database.
-
Examples:
createUser(name, email)→ Inserts a new usergetAllUsers()→ Returns all usersgetUserById(id)→ Returns a single user by IDupdateUser(id, name, email)→ Updates user infodeleteUser(id)→ Deletes a user
Note: Unlike Mongoose, we write raw SQL queries because there’s no built-in ORM in Node.js.
- Receives HTTP requests and calls model functions.
- Handles responses, errors, and validation.
- Maps HTTP endpoints to controller functions:
| HTTP Method | Endpoint | Action |
|---|---|---|
| GET | /api/users |
Get all users |
| GET | /api/users/:id |
Get user by ID |
| POST | /api/users |
Create a new user |
| PUT | /api/users/:id |
Update a user |
| DELETE | /api/users/:id |
Delete a user |
- Express server with JSON body parsing:
app.use(express.json());
app.use("/api/users", userRoutes);- Starts on port 3000.
- Install dependencies:
pnpm install express- Start the server:
node --watch server.js- The database file
data.dbwill be automatically created indb/folder. - Warning about experimental SQLite is normal in Node v22+.
curl -X GET http://localhost:3000/api/userscurl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "email": "alice@example.com"}'curl -X GET http://localhost:3000/api/users/1curl -X PUT http://localhost:3000/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "Alice Smith", "email": "alice.smith@example.com"}'curl -X DELETE http://localhost:3000/api/users/1- ID behavior: SQLite auto-increment IDs do not reuse deleted IDs.
- Database file:
data.dbcan be ignored in Git (.gitignore) for production. - Mini ORM: You can wrap models in classes to mimic Mongoose-style syntax if desired.
- VS Code: You can view the database using a SQLite extension.
- MVC structure: Works the same as Mongo/Mongoose projects; only model implementation differs.
This project shows:
- How to structure a Node.js + SQLite API using MVC.
- How to perform CRUD operations with built-in SQLite.
- How SQL differs from Mongo: more manual queries, no built-in ORM.
- How to test endpoints with cURL commands.
It’s a great starting point to learn Node.js APIs without external ORMs while maintaining a familiar MVC structure.