I'm trying to develop a server API using Koa. This server will have a single API endpoint, /api/data, which should read a local json file and display its content in the browser when a user accesses localhost:3000/api/data.
const Koa = require('koa');
const serve = require('koa-static');
const Router = require('koa-router');
// const http = require('http');
const fs = require('fs');
const app = new Koa();
const router = new Router();
app.use(serve('.'));
router.get('/api/data', async (ctx, next) => {
await fs.readFile('./data.json','utf-8', (err, data) => {
ctx.body = JSON.parse(data);
});
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
Unfortunately, I'm encountering an error with a status code of 404. Since I am relatively new to Koa, I'm not sure where I'm going wrong. Could someone please assist me with this issue?