Building REST API using Node.js & Express
In this article, we will explore how to build a REST API using Node.js and Express framework. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, allowing us to run JavaScript code as a server-side language. Express, on the other hand, is a minimal and flexible Node.js web application framework that provides a set of features to build robust APIs.
Prerequisites
Before we dive into building our REST API, make sure you have the following:
- Node.js installed on your machine. You can download it from the official website.
- A text editor of your choice. I recommend using Visual Studio Code.
Setting up the Project
To start building our REST API, follow the steps below:
- Create a new directory for your project, and navigate to it using the command line.
- Initialize a new Node.js project by running the command:
npm init -y
. This will create a newpackage.json
file. - Install Express and other necessary packages by running the command:
npm install express
. - Create a new JavaScript file, for example,
app.js
, and open it in your text editor.
Building the API
Now that we have set up our project, it’s time to build our REST API.
Open the app.js
file and add the following code:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
This code sets up a basic Express server that listens on port 3000 and responds with “Hello World!” when accessed using the root URL. Feel free to modify this code to fit your specific requirements.
Testing the API
To test the API, follow these steps:
- In the command line, navigate to the project directory.
- Start the server by running the command:
node app.js
. - Open your web browser and go to
http://localhost:3000
.
If everything is set up correctly, you should see the message “Hello World!” displayed on the webpage.
Deploying the API on Vercel
Now that we have our REST API built and tested locally, we can deploy it on Vercel for the world to access. Vercel is a cloud platform for static sites and Serverless Functions, which is an excellent option for hosting our API.
- Sign up for a Vercel account and log in.
- Click on the “New Project” button to create a new project.
- Choose your project directory and configure the settings as desired.
- Click on the “Deploy” button to deploy your API.
Vercel will provide you with a unique URL where your API is accessible. You can now use this URL to make requests to your REST API from anywhere in the world.
Conclusion
Building a REST API using Node.js and Express is a great way to create robust backend systems for your web applications. With the power of JavaScript and the simplicity of Express, you can quickly build powerful APIs to handle various types of requests. Deploying your API on Vercel ensures its accessibility to the global audience.
So go ahead, start building your own REST API using Node.js and Express, and unleash the full potential of your web applications!
Thank You very much! Of all the things I've come across on this topic, your video is the most informative.
I'm learning a lot from you. Thank you very much.
How do you handle REST methods like POST/PUT/DELETE ? I am trying those methods and Vercel seems to throw a blocked by CORS error. App works locally, just not in vercel.
vaia…….ai project ai front-end(reacctjs) soho video cai…….aha kori pabo