,

Creating a Node REST API with Express and SQLite

Posted by






Let’s Build a Node Rest API

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.


0 0 votes
Article Rating
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erhan FIRAT
1 year ago

by which progam did you open SQ DB? I watched again and again did not find any explanation about the DB application?

EriCo Academy
1 year ago

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.

YoYoYoYo
1 year ago

Thanks very much. Excellent tutorial!

Blister Fingers
1 year ago

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.

Mohamed Kouriat
1 year ago

merci cousin

Amateur2CombaT
1 year ago

Thank you very much, that was crystal clear

Roland Jorz
1 year ago

Maaaaan!!!!! … you are a fast talker, can you slow down a little bit? What's The Hurry???

spaceCabbage
1 year ago

Wow just found this channel and I’ve never heard things explained so well
Instant sub

Serhii Rumiantsev
1 year ago

awesome tutorial

Eagle Eyes
1 year ago

Wow finally someone where i dont have to set the speed to 1.5 ;-)) thanks for this. I learned a lot.

Amin Abdollah
1 year ago

Thank you very much, Very useful

Esti Proz
1 year ago

Just wow! Thank you! Liked and subscribed!

Doe
Doe
1 year ago

Great tutorial!!!
the 'patch' requests always return id:1, can you explain in details? many thanks !

Mordicai
1 year ago

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!

Eason Li
1 year ago

I am pretty glad to be taught by Jason Statham

ADO
ADO
1 year ago

2 Chronicles 1:11

WetBadger
1 year ago

"Listening on port 1337" I see what you did there.

stpharis
1 year ago

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

31S
31S
1 year ago

boring

Tanveer Brar
1 year ago

awesome tutorial with great explanation!