-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (71 loc) · 2.53 KB
/
app.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import "babel-core/register"
import "babel-polyfill"
import Koa from 'koa'
import router from './app/router/index'
import constants from './app/utils/constants'
// import mongoDb from './app/db/mongo'
import redis from './app/db/redis'
import info from './app/utils/info'
global.info = info
global.Promise = require('bluebird')
const app = new Koa()
//监听uncaughtException,防止node进程挂掉
process.on('uncaughtException', function (err) {
console.error('Unexpected exception: ' + err)
console.error('Unexpected exception stack: ' + err.stack)
})
// app.context.db = 'mydb'
// const db = require('./app/db/db')
// indexRouter.get('/getOne', async(ctx,next) => {
// app.get('/getOne', async(ctx,next) => {
// console.log('got getOne')
// // ctx.response.body = 'æ?å–? getOne'
// // ctx.body = {
// // obj :'æ?å–? getOne'
// // }
// if (ctx.request.accepts('xml')) {
// console.log('xml')
// ctx.response.type = 'xml';
// ctx.response.body = '<data>Hello World</data>';
// } else if (ctx.request.accepts('json')) {
// console.log('json')
// ctx.response.type = 'json';
// ctx.response.body = { data: 'Hello World' };
// } else if (ctx.request.accepts('html')) {
// console.log('html')
// ctx.response.type = 'html';
// ctx.response.body = '<p>Hello World</p>';
// } else {
// console.log('text')
// ctx.response.type = 'text';
// ctx.response.body = 'Hello World';
// }
// await next();
// });
app.use(async (ctx, next) => {
if (ctx.request.header.origin !== ctx.origin && constants.whiteList.includes(ctx.request.header.origin)) {
ctx.set('Access-Control-Allow-Origin', ctx.request.header.origin);
ctx.set('Access-Control-Allow-Credentials', true);
ctx.set('X-Powered-By', ' 3.2.1');
}
await next();
})
.use(async (ctx, next) => {
// console.log('OPTIONS 1')
if (ctx.method === 'OPTIONS') {
// console.log('OPTIONS 2')
ctx.set('Access-Control-Allow-Methods', 'PUT,DELETE,POST,GET,OPTIONS');
// ctx.set('Access-Control-Max-Age', 3600 * 24);
ctx.set('Access-Control-Request-Headers', 'Origin,Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,Accept');
ctx.body = '';
}
await next();
})
.use(router.routes())
.use(router.allowedMethods())
// app.on('error', err => {
// console.error('server error1111111:', err)
// });
app.listen(3333, () => {
console.log('server is running')
})