How to Build a Simple REST API with Node.js and SQLite (for Beginners)
Step 0: Letâs Get Ready
Alright kiddo, before we start coding, letâs make sure we have a few things ready:
-
Node.js and npm installed. You can check like this:
-
A code editor like VS Code (itâs like your trusty notebook for coding).
-
A good mood and a cup of coffee (or tea â) because weâre going to have fun!
Think of this like cooking: we need our ingredients ready before we start mixing things together.
Step 1: Make a Folder for Our Project
Just like having a kitchen, we need a place to cook:
Now letâs tell Node.js we have a new project:
This will create a file called package.jsonâthink of it as your recipe card for this project.
Step 2: Install Some Helpful Tools
Weâre going to need a few âtoolsâ to help us build our API:
-
Express â our helper to make the API run smoothly
-
SQLite3 â our little database to store information
-
body-parser â to read what people send to our API
Itâs like having a spatula, a pan, and a measuring cup in the kitchen.
Step 3: Create the Main File
Now weâre ready to start cooking! Create a file called server.js:
Run the server:
Open http://localhost:3000/ in your browser. You should see:
âHello, young coder! Your REST API is working. đâ
See? Easy peasy!
Step 4: Letâs Make Our API Useful
Now, we want to store some user information. Letâs make some routes to Create, Read, Update, and Delete (CRUD) users.
1. See all users
2. Add a new user
3. See a user by ID
4. Update a user
5. Delete a user
Step 5: Test It Out
Now, grab Postman or curl and try your API:
Add a new user:
See that? You just added your first user! đ
Step 6: What Next, Young Coder?
-
Try adding validation to check if the user entered a valid email
-
Organize your routes using Express Router
-
Explore other SQLite features to store more complex data
Remember, little by little, step by stepâcoding is like learning to ride a bike. You may fall sometimes, but youâll get better and faster every day! đ´ââď¸