-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support async/await * change nodejs version 4 or 6 to 5.6 * eslint * remove jade
- Loading branch information
Showing
36 changed files
with
349 additions
and
354 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
node_js: | ||
- "4" | ||
- "6" | ||
- stable | ||
- 7.6 | ||
language: node_js | ||
script: | ||
- make lint | ||
|
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
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
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
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 |
---|---|---|
@@ -1,71 +1,72 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
const render = require('./lib/render'); | ||
const logger = require('koa-logger'); | ||
const router = require('koa-router')(); | ||
const koaBody = require('koa-body'); | ||
|
||
var render = require('./lib/render'); | ||
var logger = require('koa-logger'); | ||
var route = require('koa-route'); | ||
var parse = require('co-body'); | ||
var koa = require('koa'); | ||
var app = module.exports = koa(); | ||
const Koa = require('koa'); | ||
const app = module.exports = new Koa(); | ||
|
||
// "database" | ||
|
||
var posts = []; | ||
const posts = []; | ||
|
||
// middleware | ||
|
||
app.use(logger()); | ||
|
||
// route middleware | ||
app.use(render); | ||
|
||
app.use(route.get('/', list)); | ||
app.use(route.get('/post/new', add)); | ||
app.use(route.get('/post/:id', show)); | ||
app.use(route.post('/post', create)); | ||
app.use(koaBody()); | ||
|
||
// route definitions | ||
|
||
router.get('/', list) | ||
.get('/post/new', add) | ||
.get('/post/:id', show) | ||
.post('/post', create); | ||
|
||
app.use(router.routes()); | ||
|
||
/** | ||
* Post listing. | ||
*/ | ||
|
||
function *list() { | ||
this.body = yield render('list', { posts: posts }); | ||
async function list(ctx) { | ||
await ctx.render('list', { posts: posts }); | ||
} | ||
|
||
/** | ||
* Show creation form. | ||
*/ | ||
|
||
function *add() { | ||
this.body = yield render('new'); | ||
async function add(ctx) { | ||
await ctx.render('new'); | ||
} | ||
|
||
/** | ||
* Show post :id. | ||
*/ | ||
|
||
function *show(id) { | ||
var post = posts[id]; | ||
if (!post) this.throw(404, 'invalid post id'); | ||
this.body = yield render('show', { post: post }); | ||
async function show(ctx) { | ||
const id = ctx.params.id; | ||
const post = posts[id]; | ||
if (!post) ctx.throw(404, 'invalid post id'); | ||
await ctx.render('show', { post: post }); | ||
} | ||
|
||
/** | ||
* Create a post. | ||
*/ | ||
|
||
function *create() { | ||
var post = yield parse(this); | ||
var id = posts.push(post) - 1; | ||
async function create(ctx) { | ||
const post = ctx.request.body; | ||
const id = posts.push(post) - 1; | ||
post.created_at = new Date(); | ||
post.id = id; | ||
this.redirect('/'); | ||
ctx.redirect('/'); | ||
} | ||
|
||
// listen | ||
|
||
if (!module.parent) app.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
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
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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
|
||
var koa = require('koa'); | ||
var parse = require('co-body'); | ||
const Koa = require('koa'); | ||
const koaBody = require('koa-body'); | ||
|
||
var app = module.exports = koa(); | ||
const app = module.exports = new Koa(); | ||
|
||
app.use(koaBody({ | ||
jsonLimit: '1kb' | ||
})); | ||
|
||
// POST .name to /uppercase | ||
// co-body accepts application/json | ||
// and application/x-www-form-urlencoded | ||
|
||
app.use(function *(next) { | ||
if ('POST' != this.method) return yield next; | ||
var body = yield parse(this, { limit: '1kb' }); | ||
if (!body.name) this.throw(400, '.name required'); | ||
this.body = { name: body.name.toUpperCase() }; | ||
app.use(async function(ctx) { | ||
const body = ctx.request.body; | ||
if (!body.name) ctx.throw(400, '.name required'); | ||
ctx.body = { name: body.name.toUpperCase() }; | ||
}); | ||
|
||
if (!module.parent) app.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
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
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
Oops, something went wrong.