Let’s Build a Node Rest API
In this tutorial, we will learn how to build a REST API using Node.js, Express, and SQLite.
Prerequisites
Before getting started, make sure you have the following installed:
- Node.js – Download and install Node.js from https://nodejs.org
- Express – Install Express using the following command:
npm install express
- SQLite – Install SQLite using the following command:
npm install sqlite3
Setting up the Project
First, create a new directory for your project:
mkdir node-rest-api
cd node-rest-api
Then, create a new file named server.js
:
touch server.js
Open server.js
in your preferred text editor and add the following code:
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const port = 3000;
// Configure SQLite database connection
const database = new sqlite3.Database(':memory:');
// Create a table for the API
database.run("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
app.get('/users', (req, res) => {
// Retrieve all users from the database
database.all('SELECT * FROM users', (err, rows) => {
if(err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
res.send(rows);
}
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
This code sets up an Express server, creates an in-memory SQLite database, and defines a route to retrieve all users from the database.
Testing the API
To test the API, start the server by running the following command in your terminal:
node server.js
Open your web browser and navigate to http://localhost:3000/users
. You should see an array of all users stored in the database.
Conclusion
Congratulations! You have successfully built a Node.js REST API using Express and SQLite. This API can be further expanded to include additional routes and database operations.
Feel free to explore more about Node.js, Express, and SQLite to expand your knowledge and build more advanced APIs.
by which progam did you open SQ DB? I watched again and again did not find any explanation about the DB application?
I am getting the following error when I sent data through postman.
[Error: insert into `cars` (`make`, `model`, `year`) values ('honda', 'delsol', 1995) – SQLITE_ERROR: no such table: cars] {
errno: 1,
code: 'SQLITE_ERROR'
}
I do have cars.js file in db folder.
Thanks very much. Excellent tutorial!
18:56 What exactly is the "car" object in createCar(car) and updateCar(id, car)??
What's being returned by getAllCars?
Why am I the only person asking this? It seems like a glaring omission.
merci cousin
Thank you very much, that was crystal clear
Maaaaan!!!!! … you are a fast talker, can you slow down a little bit? What's The Hurry???
Wow just found this channel and I’ve never heard things explained so well
Instant sub
awesome tutorial
Wow finally someone where i dont have to set the speed to 1.5 ;-)) thanks for this. I learned a lot.
Thank you very much, Very useful
Just wow! Thank you! Liked and subscribed!
Great tutorial!!!
the 'patch' requests always return id:1, can you explain in details? many thanks !
I appreciate the speed, you're conscientious of your viewers time, while also cramming in tons of info.
You got a sub from me sir!
I am pretty glad to be taught by Jason Statham
2 Chronicles 1:11
"Listening on port 1337" I see what you did there.
Thanks for the video. Is there a way to handle sqlite errors (i.e. wrong import query)? Currently if I get an error from sqlite (i.e. knex connection) the server ends the connection. Thank you
boring
awesome tutorial with great explanation!