index.js 812 字节
Newer Older
J
Jason Park 已提交
1 2
import express from 'express';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
J
Jason Park 已提交
3
import auth from './auth';
4
import category from './category';
J
Jason Park 已提交
5
import compiler from './compiler';
6
import wiki from './wiki';
J
Jason Park 已提交
7 8 9

const router = new express.Router();

J
Jason Park 已提交
10
router.use('/auth', auth);
11
router.use('/category', category);
J
Jason Park 已提交
12
router.use('/compiler', compiler);
13
router.use('/wiki', wiki);
J
Jason Park 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
router.use((req, res, next) => next(new NotFoundError()));
router.use((err, req, res, next) => {
  const statusMap = [
    [AuthorizationError, 401],
    [PermissionError, 403],
    [NotFoundError, 404],
    [Error, 500],
  ];
  const [, status] = statusMap.find(([Error]) => err instanceof Error);
  res.status(status);
  res.json({
    status,
    err,
  });
  console.error(err);
});

export default router;