-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (58 loc) · 1.75 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const Prismic = require('prismic-javascript');
const PrismicDOM = require('prismic-dom');
const request = require('request');
const Cookies = require('cookies');
const PrismicConfig = require('./prismic-configuration');
const Onboarding = require('./onboarding');
const app = require('./config');
const PORT = app.get('port');
app.listen(PORT, () => {
Onboarding.trigger();
process.stdout.write(`Point your browser to: http://localhost:${PORT}\n`);
});
// Middleware to inject prismic context
app.use((req, res, next) => {
res.locals.ctx = {
endpoint: PrismicConfig.apiEndpoint,
linkResolver: PrismicConfig.linkResolver,
};
// add PrismicDOM in locals to access them in templates.
res.locals.PrismicDOM = PrismicDOM;
Prismic.api(PrismicConfig.apiEndpoint, {
accessToken: PrismicConfig.accessToken,
req,
}).then((api) => {
req.prismic = { api };
next();
}).catch((error) => {
next(error.message);
});
});
/*
* --[ INSERT YOUR ROUTES HERE ]--
*/
/*
* Route with documentation to build your project with prismic
*/
app.route('/').get(function(req, res) {
req.prismic.api.getByUID('page', 'quickstart').then((document) => {
res.render('index', { document });
});
});
/*
* Preconfigured prismic preview
*/
app.get('/preview', (req, res) => {
const { token } = req.query;
if (token) {
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 });
res.redirect(302, url);
}).catch((err) => {
res.status(500).send(`Error 500 in preview: ${err.message}`);
});
} else {
res.send(400, 'Missing token from querystring');
}
});