A basic API endpoint written in Node JS using Express.
Description: an app that sends stored data when recieving a web request.
- Replace the mock data in
data.json
with your data. - Run
npm i
. - Start
index.js
.
- A user accesses the
/api
path of the app. (for examplehttp://localhost:8080/api
) - The app sends the data from
data.json
to the user in JSON format.
Example success:
{
"status": "success",
"data": {
"example data": ["some data", "some other data"],
"different data": "also data"
}
}
Example error:
{
"status": "error",
"message": "Couldn't fetch resource."
}
- Check for a key or authentication password in the header, body or parameters of the request to limit access to the API.
- Add more routes based on your needs. (Might need to create route handling (
express.Router()
,router.get
) in separate files or folders) - Add more JSON files based on your needs, or store data in variables as needed.
Tip
For an example of a full API app with multiple routes, parameters, files and folders feel free to check out my Url Shortener.