-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
34 lines (28 loc) · 842 Bytes
/
app.ts
File metadata and controls
34 lines (28 loc) · 842 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
31
32
33
34
'use strict';
import config from './app/config';
import RedisServer from './app/utils/redis';
RedisServer('myblog-ts_modules', config.redis_common); // 全局初始化redis
import Koa from 'koa'; // koa框架
import Router from 'koa-router'; // koa-router:处理路由
import cors from 'koa2-cors';
import bodyparser from 'koa-bodyparser';
import auth from './app/middleware/auth';
import routers from './app/routers';
const app = new Koa(); // 新建一个koa应用
const router = new Router(); // 新建一个路由
// 解决跨域问题
app.use(
cors({
credentials: true,
exposeHeaders: ['*'],
})
);
app.use(bodyparser());
// 全局处理错误
app.use(auth());
// 加载路由
routers(router);
app.use(router.routes());
app.listen(config.port, (): void => {
console.log(`Server running on port ${config.port}`);
});