-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi.js
More file actions
30 lines (22 loc) · 702 Bytes
/
api.js
File metadata and controls
30 lines (22 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const Koa = require('koa');
const Router = require('koa-router');
const koa404Handler = require('koa-404-handler');
const errorHandler = require('..');
// initialize our app
const app = new Koa();
// override koa's undocumented error handler
app.context.onerror = errorHandler;
// specify that this is our api
app.context.api = true;
// use koa-404-handler
app.use(koa404Handler);
// set up some routes
const router = new Router();
// throw an error anywhere you want!
router.get('/404', (ctx) => ctx.throw(404));
router.get('/500', (ctx) => ctx.throw(500));
// initialize routes on the app
app.use(router.routes());
// start the server
app.listen(3000);
console.log('listening on port 3000');