-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lienren
committed
Jan 8, 2018
1 parent
4dc8a1e
commit ccf02f2
Showing
5 changed files
with
430 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.