Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main2
  • master
2 results

Target

Select target project
  • harshith.karkera/my-express
1 result
Select Git revision
  • main2
  • master
2 results
Show changes
Commits on Source (2)
/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
});
});