41 行
1.0 KiB
TypeScript
41 行
1.0 KiB
TypeScript
import 'reflect-metadata'
|
|
import express, {NextFunction} from 'express'
|
|
import cors from 'cors'
|
|
import {appConfig} from "./config"
|
|
import {userRouter} from "./routers/user-router"
|
|
import {getPoolCreatingPromise} from "./includes"
|
|
import expressJWT from 'express-jwt'
|
|
import type {ErrorRequestHandler} from "express"
|
|
import {IResultJson} from "./types"
|
|
|
|
const app = express()
|
|
|
|
app.use(cors())
|
|
app.use(express.urlencoded({extended: true}))
|
|
app.use(
|
|
expressJWT({
|
|
algorithms: ['HS256'], secret: appConfig.jwtSecret
|
|
}).unless({path: ['/user/login']})
|
|
)
|
|
const authErrorHandler: ErrorRequestHandler =
|
|
(err,
|
|
req
|
|
, res
|
|
, next) => {
|
|
if (err.name == 'UnauthorizedError') {
|
|
res.status(401).send(<IResultJson>{status: false, data: '使用者未登入'})
|
|
}
|
|
}
|
|
app.use(authErrorHandler)
|
|
|
|
app.use('/user', userRouter)
|
|
|
|
async function entrypoint() {
|
|
await getPoolCreatingPromise()
|
|
app.listen(appConfig.port, () => {
|
|
console.log('Server started at port ' + appConfig.port)
|
|
})
|
|
}
|
|
|
|
entrypoint()
|