Master API Testing with Node.js and Express: Your Ultimate Guide to Building and Testing APIs
Kundan Antyakula??
Devops Engineer - Data & Infrastructure Specialist | AWS Certified (2x) | GitHub Certified (1x) | Kubernetes & Containerization | CI/CD & Infrastructure Automation | Driving Secure Data & Scalable DevOps Solutions
Why Test APIs?
API testing is crucial because it ensures that your API works as expected and handles different kinds of requests correctly. This is important for several reasons:
Tools for Testing APIs
There are various tools you can use to test APIs, ranging from command-line utilities to sophisticated GUI applications. Here are a few commonly used ones:
Let's Proceed with building our API server first
Step 1: Initialize and Install Express
Before we start with the code, we initialize a Node.js project and install Express.
Initialize a Node.js project:
npm init -y
This command creates a?package.json?file with default settings for your project.
Install Express:
npm install express
This command installs Express, a web application framework for Node.js.
Create a new file index.js comprising the below code
const express = require('express');
const app = express();
const PORT = 8080;
// Middleware to parse JSON bodies
app.use(express.json());
// GET endpoint
app.get('/kundan', (req, res) => {
res.status(200).send({
kundan: '??',
size: 'large'
});
});
// POST endpoint with parameter
app.post('/kundan/:id', (req, res) => {
const { id } = req.params;
const { logo } = req.body;
// Check if logo exists in request body
if (!logo) {
return res.status(418).send({ message: 'We need a logo!' });
}
// Send response with interpolated string
res.send({
kundan: `?? with your ${logo} and ID of ${id}`,
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on https://localhost:${PORT}`);
});
Let's Break this code as chunks and explain with complete detailing and analysis
Step 2: Creating the Server
const express = require('express');
const app = express();
const PORT = 8080;
Middleware to Parse JSON
app.use(express.json());
GET Endpoint
app.get('/kundan', (req, res) => {
res.status(200).send({
kundan: '??',
size: 'large'
});
});
领英推荐
POST Endpoint with Parameter
app.post('/kundan/:id', (req, res) => {
const { id } = req.params;
const { logo } = req.body;
if (!logo) {
return res.status(418).send({ message: 'We need a logo!' });
}
res.send({
kundan: `?? with your ${logo} and ID of ${id}`,
});
});
Starting the Server
app.listen(PORT, () => {
console.log(`Server is running on https://localhost:${PORT}`);
});
Running the Server
To start the server, use the following command:
node index.js
This command runs the?index.js?file, starting the server on?https://localhost:8080.
Test the GET endpoint:
curl https://localhost:8080/kundan
Test the POST endpoint:
curl -X POST https://localhost:8080/kundan/504 -H "Content-Type: application/json" -d '{"logo": "?"}'
What the Server Does with This Request
The server processes this request as follows:
app.post('/kundan/:id', (req, res) => {
const { id } = req.params;
const { logo } = req.body;
if (!logo) {
return res.status(418).send({ message: 'We need a logo!' });
}
res.send({
kundan: `?? with your ${logo} and ID of ${id}`,
});
});
{"kundan":"?? with your ? and ID of 504"}%
In conclusion, testing APIs is essential for ensuring functionality, reliability, performance, and security. By verifying that each endpoint works as intended, handles requests appropriately, performs well under varying conditions, and remains secure against vulnerabilities, API testing guarantees a robust and dependable application interface.