A basic API endpoint written in Node.js using Express.
A simple app that returns stored data in response to web requests.
- Replace the mock data in
data.jsonwith your data. - Run
npm i. - Start
index.js.
- A user accesses the
/apiendpoint (for examplehttp://localhost:8080/api). - The app sends the data from
data.jsonto 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 request header, body, or query parameters to limit access.
- Add more routes as needed (may require using
express.Router()and separate route files/folders). - Add more JSON files or store data in variables to expand your API.
Tip
For a more complete example with multiple routes, parameters, and files/folders, see my Url Shortener project.