Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Codio-Deno-REST-API-Template/index.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (43 sloc)
1.11 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* index.js */ | |
import { Application, Router, Status } from 'https://deno.land/x/oak/mod.ts' | |
import { parse } from 'https://deno.land/std/flags/mod.ts' | |
import router from './routes.ts' | |
const defaultPort = 8080 | |
const { args } = Deno | |
const argPort = parse(args).port | |
const port = argPort ? Number(argPort) : defaultPort | |
const app = new Application() | |
// error handler | |
app.use(async (context, next) => { | |
try { | |
console.log(context.request.url.href) | |
context.response.headers.set('Content-Type', 'application/json') | |
await next() | |
} catch (err) { | |
console.log(err) | |
} | |
}) | |
app.use(router.routes()) | |
app.use(router.allowedMethods()) | |
// static content | |
// app.use(async (context, next) => { | |
// const root = `${Deno.cwd()}/public` | |
// try { | |
// await context.send({ root }) | |
// } catch { | |
// next() | |
// } | |
// }) | |
// page not found | |
app.use( async context => { | |
try { | |
console.log('404 PAGE NOT FOUND') | |
context.response.body = { status: 'error', msg: 'page not found' } | |
} catch(err) { | |
console.error(err) | |
} | |
}) | |
app.addEventListener('listen', ({ port }) => { | |
console.log(`listening on port: ${port}`) | |
}) | |
await app.listen({ port }) |