Skip to content
Snippets Groups Projects
Commit de584176 authored by Harshith Karkera's avatar Harshith Karkera
Browse files

Merge branch 'main' into 'master'

Initial commit: api creation get and post

See merge request !1
parents 06bc8c4e 01af59b0
Branches master
No related tags found
1 merge request!1Initial commit: api creation get and post
/node_modules
\ No newline at end of file
Express.js API Endpoints
GET API
URL: http://localhost:3000/info/:pathParam?queryParam=value
Method: GET
Description: Returns both query parameter and path parameter as JSON.
Example: http://localhost:3000/info/123?queryParam=hello
expected response:
{
"message": "GET API Response",
"pathParam": "123",
"queryParam": "hello"
}
-----------------------------------------------------------------------------
POST API
URL: http://localhost:3000/data
Method: POST
Description: Accepts JSON data in the request body and returns an array of data.
Headers: Content-Type: application/json
Post Data (request body json):
{
"name": "Vijay",
"age": 25
}
expected response:
{
"message": "POST API Response",
"data": [
{
"name": "Vijay",
"age": 25
}
]
}
\ No newline at end of file
This diff is collapsed.
{
"name": "my-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.21.2"
}
}
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// GET API with query and path parameters
app.get('/info/:pathParam', (req, res) => {
const pathParam = req.params.pathParam;
const queryParam = req.query.queryParam;
res.json({
message: "GET API Response",
pathParam: pathParam,
queryParam: queryParam || "No query param provided"
});
});
// POST API that accepts JSON data and returns an array
app.post('/data', (req, res) => {
const inputData = req.body;
if (!inputData || Object.keys(inputData).length === 0) {
return res.status(400).json({ message: "No data provided" });
}
const responseArray = Array.isArray(inputData) ? inputData : [inputData];
res.json({
message: "POST API Response",
data: responseArray
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment