add backend
This commit is contained in:
parent
bbbabdacdb
commit
ee37440442
18
.eslintrc.cjs
Normal file
18
.eslintrc.cjs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
root: true,
|
||||||
|
env: { browser: true, es2020: true },
|
||||||
|
extends: [
|
||||||
|
'eslint:recommended',
|
||||||
|
'plugin:@typescript-eslint/recommended',
|
||||||
|
'plugin:react-hooks/recommended',
|
||||||
|
],
|
||||||
|
ignorePatterns: ['dist', '.eslintrc.cjs'],
|
||||||
|
parser: '@typescript-eslint/parser',
|
||||||
|
plugins: ['react-refresh'],
|
||||||
|
rules: {
|
||||||
|
'react-refresh/only-export-components': [
|
||||||
|
'warn',
|
||||||
|
{ allowConstantExport: true },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
3022
api/data.json
Normal file
3022
api/data.json
Normal file
File diff suppressed because it is too large
Load Diff
32
api/main.ts
Normal file
32
api/main.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { Application, Router } from "@oak/oak";
|
||||||
|
import { oakCors } from "@tajpouria/cors";
|
||||||
|
import data from "./data.json" with { type: "json" };
|
||||||
|
|
||||||
|
const router = new Router();
|
||||||
|
|
||||||
|
router.get("/", (context) => {
|
||||||
|
context.response.body = "Welcome to dinosaur API!";
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get("/dinosaurs", (context) => {
|
||||||
|
context.response.body = data;
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get("/dinosaurs/:dinosaur", (context) => {
|
||||||
|
if (!context?.params?.dinosaur) {
|
||||||
|
context.response.body = "No dinosaur name provided.";
|
||||||
|
}
|
||||||
|
|
||||||
|
const dinosaur = data.find((item) =>
|
||||||
|
item.name.toLowerCase() === context.params.dinosaur.toLowerCase()
|
||||||
|
);
|
||||||
|
|
||||||
|
context.response.body = dinosaur ? dinosaur : "No dinosaur found.";
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = new Application();
|
||||||
|
app.use(oakCors());
|
||||||
|
app.use(router.routes());
|
||||||
|
app.use(router.allowedMethods());
|
||||||
|
|
||||||
|
await app.listen({ port: 8000 });
|
||||||
Loading…
Reference in New Issue
Block a user