I am trying to add a new route to keystonejs, because my intention is to have two contextroots using recaptcha for spamming protection.
What I did to add the route:
In my keystone.js file there is a line: keystone.set('routes', require('./routes'));
so I added:
keystone.set('protectedroutes', require('./protectedroutes'));
because the name of my folder is "protectedroutes".
My part in the middleware.js:
exports.initContributes = function (req, res, next) {
if (!(req.body['g-recaptcha-response'] && req.body['g-recaptcha-response'].length)) {
return res.json({
"responseCode": 1,
"responseDesc": "Please select captcha"
});
}
if (validateRecaptcha(req.body['g-recaptcha-response'], req.connection.remoteAddress)) {
next();
} else {
return res.json({
"responseCode": 1,
"responseDesc": "Failed captcha verification"
});
}
};
My whole index.js:
var keystone = require('keystone');
var middleware = require('./middleware');
var importRoutes = keystone.importer(__dirname),
importProtectedRoutes = keystone.importer('./protectedroutes/');
// Common Middleware
keystone.pre('routes', middleware.initLocals);
keystone.pre('protectedroutes', middleware.initContributes);
keystone.pre('render', middleware.flashMessages);
// Import Route Controllers
var routes = {
views: importRoutes('./views')
},
protectedRoutes = {
views: importRoutes('./views')
};
// Setup Route Bindings
exports = module.exports = function (app) {
// Views
app.get('/', routes.views.index);
app.get('/blog/:category?', routes.views.blog);
app.get('/blog/post/:post', routes.views.post);
app.get('/lockoverview/:category?', routes.views.lockoverview);
app.get('/systemoverview/:category?', routes.views.systemoverview);
app.get('/tooloverview/:category?', routes.views.tooloverview);
app.get('/lockoverview/lock/:lock', routes.views.lock);
app.get('/systemoverview/system/:system', routes.views.system);
app.get('/tooloverview/tool/:tool', routes.views.tool);
app.get('/gallery', routes.views.gallery);
app.all('/contact', routes.views.contact);
app.all('/contributelock', protectedRoutes.views.contributelock);
app.all('/contributetool', protectedRoutes.views.contributetool);
// NOTE: To protect a route so that only admins can see it, use the requireUser middleware:
// app.get('/protected', middleware.requireUser, routes.views.protected);
};
Error: if(cache.opts.strict) throw new Error('Hooks for ' + hook + ' are not supported.'); Error: Hooks for pre: protectedroutes are not supported.
It is caused by this line:
keystone.pre('protectedroutes', middleware.initContributes);
What am I doing wrong?
via SiriSch
No comments:
Post a Comment