APIs (Application Programming Interfaces) are the backbone of modern web applications, allowing communication between different services and applications. If you’re new to API development, building a simple endpoint using Node.js and Express.js is an excellent way to get started. In this guide, you’ll learn how to set up a basic API endpoint that returns JSON data, making it easy for you to grasp the fundamentals of API development.
What is an Endpoint in a REST API?
An endpoint is a specific URL where an API receives requests and sends responses. Each endpoint is associated with a particular HTTP method (such as GET, POST, PUT, DELETE) and is used to interact with the API.
For example, if you are building a food ordering app, an API endpoint like:
GET /restaurants
could return a list of available restaurants.
In this tutorial, we’ll build a GET endpoint that returns sample data.
Also read: How to Set Up a Simple Node.js Server in 5 Minutes
Setting Up an Express.js Route for a GET Request
Step 1: Install Node.js and Express
First, ensure you have Node.js installed on your machine. If not, download it from nodejs.org.
Then, create a project directory and initialize a Node.js project:
mkdir my-api cd my-api npm init -y
Next, install Express.js:
npm install express
Step 2: Create a Basic Express Server
Create an index.js file and add the following code:
const express = require('express'); const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Welcome to my first API!'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
Now, run the server:
node index.js
Visit http://localhost:3000 in your browser, and you should see Welcome to my first API! displayed.
Returning Data as a JSON Response
Instead of sending plain text, let’s return JSON data. Modify the index.js file to create a new GET endpoint:
app.get('/api/data', (req, res) => { const sampleData = { name: "John Doe", age: 30, occupation: "Software Developer" }; res.json(sampleData); });
Restart your server and visit http://localhost:3000/api/data. You should see:
{ "name": "John Doe", "age": 30, "occupation": "Software Developer" }
Testing the Endpoint with Postman or a Browser
Testing with a Browser
For simple GET requests, you can directly visit http://localhost:3000/api/data in your browser, and the JSON response will be displayed.
Testing with Postman
For more advanced testing, download Postman and follow these steps:
- Open Postman and create a new GET request.
- Enter http://localhost:3000/api/data in the request URL.
- Click “Send.”
- You should see the JSON response displayed in Postman.
Read: How to create a REST API with Node.js and Express
Conclusion
Congratulations! 🎉 You’ve successfully built your first API endpoint using Node.js and Express.js. You now understand:
- What an API endpoint is.
- How to create a basic Express server.
- How to return JSON data in a response.
- How to test your API using Postman or a browser.
From here, you can expand your API by adding more endpoints, handling different HTTP methods, and connecting to a database. Happy coding!