mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 06:46:09 +08:00
56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
import { Request, Response, NextFunction, Application } from 'express';
|
|
import bodyParser from 'body-parser';
|
|
import cors from 'cors';
|
|
import routes from '../api';
|
|
import config from '../config';
|
|
import jwt from 'express-jwt';
|
|
|
|
export default ({ app }: { app: Application }) => {
|
|
app.enable('trust proxy');
|
|
app.use(cors());
|
|
|
|
app.use(bodyParser.json());
|
|
app.use(
|
|
jwt({ secret: config.secret as string, algorithms: ['HS384'] }).unless({
|
|
path: ['/api/auth'],
|
|
}),
|
|
);
|
|
app.use(config.api.prefix, routes());
|
|
|
|
app.use((req, res, next) => {
|
|
const err: any = new Error('Not Found');
|
|
err['status'] = 404;
|
|
next(err);
|
|
});
|
|
|
|
app.use(
|
|
(
|
|
err: Error & { status: number },
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction,
|
|
) => {
|
|
if (err.name === 'UnauthorizedError') {
|
|
return res.status(err.status).send({ message: err.message }).end();
|
|
}
|
|
return next(err);
|
|
},
|
|
);
|
|
|
|
app.use(
|
|
(
|
|
err: Error & { status: number },
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction,
|
|
) => {
|
|
res.status(err.status || 500);
|
|
res.json({
|
|
errors: {
|
|
message: err.message,
|
|
},
|
|
});
|
|
},
|
|
);
|
|
};
|