Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created March 6, 2021 22:44
Show Gist options
  • Save mayankchoubey/e40bf509ff9e2c03f559cec2fdaf025a to your computer and use it in GitHub Desktop.
Save mayankchoubey/e40bf509ff9e2c03f559cec2fdaf025a to your computer and use it in GitHub Desktop.
import { serve } from "https://deno.land/std/http/server.ts";
const get1 = JSON.parse(await Deno.readTextFile("./getData.json"));
const get2 = JSON.parse(await Deno.readTextFile("./getData2.json"));
const post1 = JSON.parse(await Deno.readTextFile("./postData.json"));
const s = serve({ port: 3000 });
for await (const req of s) {
console.log(req.url);
switch(req.method) {
case 'GET': {
switch(req.url) {
case '/': {
req.respond({ status: 200,
body: JSON.stringify(get1) });
break;
}
case '/another': {
req.respond({ status: 200,
body: JSON.stringify(get2) });
break;
}
}
break;
}
case 'POST': {
switch(req.url) {
case '/': {
req.respond({ status: 200,
body: JSON.stringify(post1) });
break;
}
}
}
}
}
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const get1 = JSON.parse(await Deno.readTextFile("./getData.json"));
const get2 = JSON.parse(await Deno.readTextFile("./getData2.json"));
const post1 = JSON.parse(await Deno.readTextFile("./postData.json"));
const router = new Router();
router
.get('/', ctx => {
ctx.response.body=get1;
})
.get('/another', ctx => {
ctx.response.body=get2;
})
.post('/', ctx => {
ctx.response.body=post1;
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 3000 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment