-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
executable file
·142 lines (115 loc) · 3.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Module dependencies.
*/
var Prismic = require('prismic-nodejs');
var app = require('./config');
var PORT = app.get('port');
var PConfig = require('./prismic-configuration');
/**
* Start the server
*/
app.listen(PORT, function() {
console.log('Point your browser to http://localhost:' + PORT);
});
/**
* Middleware to connect to the API
*/
app.use((req, res, next) => {
Prismic.api(PConfig.apiEndpoint,{accessToken: PConfig.accessToken, req: req})
.then((api) => {
req.prismic = {api: api};
res.locals.ctx = {
endpoint: PConfig.apiEndpoint,
linkResolver: PConfig.linkResolver
};
next();
}).catch(function(err) {
if (err.status == 404) {
res.status(404).send('There was a problem connecting to your API, please check your configuration file for errors.');
} else {
res.status(500).send('Error 500: ' + err.message);
}
});
});
/**
* Preconfigured prismic preview
*/
app.get('/preview', function(req, res) {
return Prismic.preview(req.prismic.api, PConfig.linkResolver, req, res);
});
/**
* Query the navigation with every route
*/
app.use((req, res, next) => {
// Set the language for the navigation
var lang = 'en-us';
if (req.url.match(/\/fr-fr\/*/g)) {
lang = 'fr-fr';
} else if (req.url.match(/\/es-es\/*/g)) {
lang = 'es-es';
}
// Query the navigation by language
req.prismic.api.getSingle('navigation', {lang : lang}).then(function(navContent){
// Give an error if no layout custom type is found
if (!navContent) {
res.status(500).send('No Layout document was found.');
}
// Define the layout content
res.locals.navContent = navContent;
next();
});
});
/**************************** FRENCH SITE ****************************/
/**
* Route for the french homepage
*/
app.route('/fr-fr').get(function(req, res){
req.prismic.api.getSingle("homepage", {lang : 'fr-fr'}).then(function(pageContent) {
res.render('homepage', { pageContent: pageContent });
});
});
/**
* Route for french pages
*/
app.route('/fr-fr/:uid').get(function(req, res) {
var uid = req.params.uid;
req.prismic.api.getByUID("page", uid, {lang : 'fr-fr'}).then(function(pageContent) {
res.render('page', { pageContent: pageContent });
});
});
/**************************** SPANISH SITE ****************************/
/**
* Route for the spanish homepage
*/
app.route('/es-es').get(function(req, res){
req.prismic.api.getSingle("homepage", {lang : 'es-es'}).then(function(pageContent) {
res.render('homepage', { pageContent: pageContent });
});
});
/**
* Route for spanish pages
*/
app.route('/es-es/:uid').get(function(req, res) {
var uid = req.params.uid;
req.prismic.api.getByUID("page", uid, {lang : 'es-es'}).then(function(pageContent) {
res.render('page', { pageContent: pageContent });
});
});
/**************************** ENGLISH SITE ****************************/
/**
* Route for english pages
*/
app.route('/:uid').get(function(req, res) {
var uid = req.params.uid;
req.prismic.api.getByUID("page", uid, {lang : 'en-us'}).then(function(pageContent) {
res.render('page', { pageContent: pageContent });
});
});
/**
* Route for the homepage
*/
app.route('/').get(function(req, res){
req.prismic.api.getSingle("homepage", {lang : 'en-us'}).then(function(pageContent) {
res.render('homepage', { pageContent: pageContent });
});
});