Created
November 1, 2017 20:45
-
-
Save moiz-frost/825024b4951fef71293022235e637bf7 to your computer and use it in GitHub Desktop.
Bug
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
var app = require('koa')() | |
, logger = require('koa-logger') | |
, json = require('koa-json') | |
, views = require('koa-views') | |
, onerror = require('koa-onerror') | |
, session = require('koa-session'); | |
var mongoose = require(__dirname + '/database/connection'); | |
// all routes - TODO | |
var allRoutes = require(__dirname + '/routes/combinedRoutes'); | |
// error handler | |
onerror(app); | |
// global middleware | |
app | |
.use(views('views', { | |
root: __dirname + '/views', | |
default: 'jade' | |
})) | |
.use(require('koa-bodyparser')()) | |
.use(json()) | |
.use(logger()) | |
.use(function *(next){ | |
console.log(allRoutes); | |
var start = new Date; | |
yield next; | |
var ms = new Date - start; | |
console.log('%s %s - %s', this.method, this.url, ms); | |
}) | |
.use(require('koa-static')(__dirname + '/public')) | |
.use(session(app)) | |
// routes definition | |
.use(allRoutes()) // TODO | |
// error-handling | |
.on('error', (err, ctx) => { | |
console.error('server error', err, ctx) | |
}); | |
module.exports = app; |
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
// TODO | |
var compose = require('koa-compose'); | |
// import routes | |
var indexRouter = require(__dirname + '/index') | |
, usersRouter = require(__dirname + '/users'); | |
// takes an array of routers | |
function combineRouters (routers) { | |
if (!Array.isArray(routers)) { | |
routers = Array.prototype.slice.call(arguments); | |
}; | |
var middleware = []; | |
routers.forEach(function (router) { | |
middleware.push(router.routes()); | |
middleware.push(router.allowedMethods()); | |
}); | |
return compose(middleware); | |
} | |
var combinedRoutes = combineRouters([ | |
indexRouter, | |
usersRouter | |
]); | |
module.exports = combinedRoutes; |
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
var router = require('koa-router')(); | |
router.get('/', function *(next) { | |
yield this.render('index', { | |
title: 'Hello World Koa!' | |
}); | |
}); | |
router.get('/foo', function *(next) { | |
yield this.render('index', { | |
title: 'Hello World foo!' | |
}); | |
}); | |
module.exports = router; |
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
var router = require('koa-router')(); | |
router.prefix('/users'); | |
router.get('/', function *(next) { | |
this.body = 'this is a users response!'; | |
}); | |
router.get('/bar', function *(next) { | |
this.body = 'this is a users/bar response!'; | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment