Building a Basic REST API with Express.js & Firebase in Node.js
Create a powerful backend for your web app with Express, Firebase, and Node JS
Welcome to my new tutorial on building REST API with Express JS in Node JS. REST API stands for Representational State Transfer API, allowing you to transfer data. They are often used to connect a database and provide data to the client side or to communicate with different applications. For example, if you have a to-do list app that stores the to-do list in the database. You could use a REST API to handle this.
In this tutorial, we will use Express, firebase, and Node to build server-side applications using JavaScript. Firebase is a Google-backed service to simplify backend development, Offering tools like database, storage, and many more. As for Express, it's a popular node JS library for building APIs and web applications with Node JS. Together, they can help you build a REST API with ease.
Getting started
For getting started lets first create a firebase project.
- Create a Firebase project. You can do this by going to the Firebase console and clicking "Add project"
Then enable firebase firestore
Now create a collection called
todos
in you firestorNow lets create a express project
npm init -y
This makes a basic package.json
file for your project. Now install the dependencies you need for the project:
npm install express firebase
Creating a Express app
You can do this by creating a new file called index.js
and adding the following code:
const express = require('express');
const firebase = require('firebase');
const app = express();
// Initialize Firebase
const firebaseConfig = {
// Your Firebase configuration goes here
};
firebase.initializeApp(firebaseConfig);
// Start the server
app.listen(3000, () => { console.log('Server listening on port 3000'); });
Getting to do's
Now in the express app you can create a simple route to get all to do's. You can do this by adding the following code to your index.js
file:
app.get('/todos', async (req, res) => {
const todosCollection = firestore.collection('todos');
const todos = await todosCollection.get();
const todosData = todos.docs.map((doc) => doc.data());
res.send(todosData);
});
Here is a full example of your app:
const express = require('express');
const firebase = require('firebase');
const app = express();
// Initialize Firebase
const firebaseConfig = {
// Your Firebase configuration goes here
};
firebase.initializeApp(firebaseConfig);
app.get('/todos', async (req, res) => {
const todosCollection = firestore.collection('todos');
const todos = await todosCollection.get();
const todosData = todos.docs.map((doc) => doc.data());
res.send(todosData);
});
// Start the server
app.listen(3000, () => { console.log('Server listening on port 3000'); });
Now lets start the API by running this command in your terminal.
node index.js
Now, you can now access your REST API at http://localhost:3000/todos
.
Adding more feature
You can add more features to your REST API, such as the ability to create, or delete a task, by adding additional routes to your index.js
file. Here is example on how you can create a task with the API.
app.post('/todos', async (req, res) => {
const todo = req.body;
await todosCollection.add(todo);
res.send('Todo created successfully!');
});
Conclusion
Hurray you did it. That's how you can create a basic REST API. Now you can expand your API by adding more hooks or authentication to protect your database even further. In this tutorial you can create a Firebase project first then you created a collection in it. Then, you created an express API continuing with adding routes to the API, and yes that is what you did. Thanks for reading the tutorial.