UNPKG

prismic-website

Version:

A ready to use webserver (Express base) with utility methods for working with Prismic.io.

36 lines (30 loc) 1.05 kB
var Prismic = require('prismic.io').Prismic; var config = require(__dirname + '/../../config'); // Router middleware adds a Prismic context to the res object module.exports = function(req, res, next) { Prismic.Api(config.apiEndpoint, function(err, Api) { if (err) { console.log('prismic middleware ERROR'); return res.send(500, 'Error 500: ' + err.message); } var ref = req.query['ref'] || Api.master(); var ctx = { api: Api, ref: ref, maybeRef: ref == Api.master() ? undefined : ref, oauth: function() { var token = accessToken; return { accessToken: token, hasPrivilegedAccess: !!token } } }; res.locals.ctx = ctx; next(); }, config.accessToken, undefined, // request handler undefined, // api cache 5); // cache TTL in seconds };