-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1.04 KB
/
index.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
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const cors = require('@koa/cors');
const corsOptions = {
origin: '*',
allowHeaders: ['Access-Control-Allow-Headers', 'Origin','Accept', 'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Auth'],
allowMethods: ['GET','HEAD','OPTIONS', 'PUT','POST','DELETE','PATCH'],
};
const app = new Koa();
app.use(cors(corsOptions));
app.use(bodyParser());
const router = new Router();
router
.get('/', (ctx, next) => {
console.log('[i] GET /')
ctx.body = {
message: 'Hello World!'
};
})
.get('/header', (ctx, next) => {
console.log('[i] GET /header')
ctx.body = {
header: { Auth: ctx.request.get('Auth') },
}
})
.post('/', (ctx, next) => {
console.log('[i] POST /')
// handle your post request here
ctx.body = ctx.request.body;
})
app
.use(router.routes())
.use(router.allowedMethods());
console.log('Listening port 3001');
app.listen(3001);