page-express-mapper
Version:
Plugin for page.js which directly imitates the Express API.
58 lines (51 loc) • 1.54 kB
JavaScript
const page = require('page')
function pageExpressMapper (params) {
let router, res
// renderMethod param
if (params.renderMethod && typeof params.renderMethod === 'function') {
res = {
render: params.renderMethod, // template, model
redirect: function (route) {
page.redirect(route)
}
}
// overload page.js route prototype
page.Route.prototype.middleware = function (fn) {
const self = this
return function (ctx, next) {
if (self.match(ctx.path, ctx.params)) return fn(ctx, res, next) // the method is the same except this line was modified
next()
}
}
}
// customRouter param
if (params.customRouter) {
router = params.customRouter
} else {
router = {
route: function (route) {
return {
get: function (...callbacks) {
page(route, ...callbacks)
router.stack[route] = router.stack[route] || {}
router.stack[route].get = true
},
post: function (...callbacks) {
page(route, ...callbacks)
router.stack[route] = router.stack[route] || {}
router.stack[route].post = true
},
all: function (...callbacks) {
page(route, ...callbacks)
router.stack[route] = router.stack[route] || {}
router.stack[route].get = true
router.stack[route].post = true
}
}
}
}
}
router.stack = {}
return router
}
module.exports = pageExpressMapper