Skip to content

Commit

Permalink
Create files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lienren committed Jan 8, 2018
1 parent 4dc8a1e commit ccf02f2
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @Author: Lienren
* @Date: 2018-01-02 14:28:21
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-02 15:18:14
*/

const http = require('http');
const path = require('path');
const koa = require('koa');
const static = require('koa-static');
const cors = require('koa2-cors');
const bodyParser = require('koa-bodyparser');
const app = new koa();

// 静态存放地址
const staticPath = './static';
app.use(static(path.join(__dirname, staticPath)));

// 配置跨域访问
app.use(
cors({
origin: function(ctx) {
if (ctx.url === '/test') {
return false;
}
return '*';
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept']
})
);

// 使用koa-bodyparser中间件
app.use(bodyParser());

// 使用路由
const router = require('./router');
app.use(router);

// 绑定访问端口
http.createServer(app.callback()).listen(3000);
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"koa": "^2.4.1",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.3.0",
"koa-static": "^4.0.2",
"koa2-cors": "^2.0.5"
}
}
21 changes: 21 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @Author: Lienren
* @Date: 2018-01-08 16:21:55
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 16:21:55
*/
const Router = require('koa-router');
const router = new Router();

router
.get('/', async (ctx, next) => {
ctx.body = `<h1>你好</h1>`;
})
.post('/create', async (ctx, next) => {
console.log(ctx.request.body);
ctx.body = {
...ctx.request.body
};
});

module.exports = router.routes();
Binary file added static/demo1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ccf02f2

Please sign in to comment.