Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Rename config to PrismicConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
srenault committed Jun 8, 2017
1 parent 14c0cec commit 1700501
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Prismic = require('prismic-nodejs');
const request = require('request');
const Cookies = require('cookies');
const config = require('./prismic-configuration');
const PrismicConfig = require('./prismic-configuration');
const Onboarding = require('./onboarding');
const app = require('./config');

Expand All @@ -12,29 +12,28 @@ app.listen(PORT, () => {
process.stdout.write(`Point your browser to: http://localhost:${PORT}\n`);
});

// Middleware catch all request, query Prismic API and configure everything for it
// Middleware to inject prismic context
app.use((req, res, next) => {
// init prismic context
res.locals.ctx = {
endpoint: config.apiEndpoint,
linkResolver: config.linkResolver,
endpoint: PrismicConfig.apiEndpoint,
linkResolver: PrismicConfig.linkResolver,
};
Prismic.api(config.apiEndpoint, {
accessToken: config.accessToken,

Prismic.api(PrismicConfig.apiEndpoint, {
accessToken: PrismicConfig.accessToken,
req,
})
.then((api) => {
}).then((api) => {
req.prismic = { api };
// continue spreading request
next();
})
.catch((error) => {
// next with params handle error natively in express
}).catch((error) => {
next(error.message);
});
});

// INSERT YOUR ROUTES HERE
/*
* --[ INSERT YOUR ROUTES HERE ]--
*/

/*
* Route with documentation to build your project with prismic
*/
Expand All @@ -47,7 +46,7 @@ app.get('/', (req, res) => {
*/
app.get('/help', (req, res) => {
const repoRegexp = new RegExp('^(https?://([\\-\\w]+)\\.[a-z]+\\.(io|dev))/api$');
const match = config.apiEndpoint.match(repoRegexp);
const match = PrismicConfig.apiEndpoint.match(repoRegexp);
const repoURL = match[1];
const name = match[2];
const host = req.headers.host;
Expand All @@ -58,11 +57,10 @@ app.get('/help', (req, res) => {
/*
* Preconfigured prismic preview
*/
// preview
app.get('/preview', (req, res) => {
const token = req.query.token;
if (token) {
req.prismic.api.previewSession(token, config.linkResolver, '/')
req.prismic.api.previewSession(token, PrismicConfig.linkResolver, '/')
.then((url) => {
const cookies = new Cookies(req, res);
cookies.set(Prismic.previewCookie, token, { maxAge: 30 * 60 * 1000, path: '/', httpOnly: false });
Expand Down

0 comments on commit 1700501

Please sign in to comment.