-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
151 lines (132 loc) · 4.49 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
141
142
143
144
145
146
147
148
149
150
151
/*eslint-disable no-undef, no-implicit-globals*/
import { config } from 'dotenv';
config();
import createError from 'http-errors';
import express, { urlencoded } from 'express';
import { join } from 'path';
import cookieParser from 'cookie-parser';
import BodyParser from 'body-parser';
import logger from 'morgan';
import { formatPrice } from './public/scripts/server.js';
const supportedLangs = ['en-US', 'es-ES'];
const languageNames = ['English', 'Spanish'];
import algoliasearch from 'algoliasearch/lite.js';
import ArticleHelper from './helpers/article-helper.js';
import path from 'path';
import { fileURLToPath } from 'url';
import webhook from './routes/webhook.js';
import push from './routes/push.js';
import push_cm from './routes/push_cm.js'
import subscribe from './routes/subscribe.js';
import index from './routes/index.js';
import cafes from './routes/cafes.js';
import articles from './routes/articles.js';
import aboutUs from './routes/about-us.js';
import contacts from './routes/contacts.js';
import store from './routes/store.js';
import coffee from './routes/coffee.js';
import brewer from './routes/brewer.js';
import search from './routes/search.js';
import { resolveRichText } from './resolvers/rich-text-resolver.js'
const { raw } = BodyParser;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(logger('dev'));
app.use(urlencoded({ extended: false }));
app.use(cookieParser());
app.use(raw({type:'application/json'}));
app.use(express.static(join(__dirname, 'public')));
//allow culture data access in views
app.locals.supportedLangs = supportedLangs;
app.locals.languageNames = languageNames;
//allow access in routers
app.set('supportedLangs', supportedLangs);
//view engine setup
app.set('views', join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use('/', webhook);
app.use('/', push);
app.use('/', push_cm)
app.use('/', subscribe);
//force language prefix
app.use(['/:lang/*', '/:lang', '/'], function (req, res, next) {
//save full URL for use in site header
app.locals.currentURL = req.originalUrl;
if(req.originalUrl.includes('/public/')) next();
const lang = req.params.lang ? req.params.lang : '';
if(supportedLangs.includes(lang)) {
/*
* prefix was found, set culture.
* app.set() available in middleware,
* app.locals available in views
*/
app.set('currentCulture', lang);
app.locals.currentCulture = lang;
next();
}
else {
//prefix not found in route- use default (first in list)
res.redirect(`/${supportedLangs[0]}${req.originalUrl}`);
}
});
//generate Algolia index
app.use('/:lang/algolia', (req, res, _next) => {
const client = algoliasearch(process.env.algoliaApp, process.env.algoliaKey);
const index = client.initIndex(process.env.indexName);
//set index settings
index.setSettings({
'attributesForFaceting': [
'language',
'type'
],
'typoTolerance': false,
'searchableAttributes': [
'bodyCopy',
'summary',
]
});
const allArticles = supportedLangs.map(lang => ArticleHelper.getAllArticles(lang, true).then(result => result.data.items));
Promise.all(allArticles).then(result => {
//add all articles to index
result.flat(1).forEach(article => {
index.saveObject({
objectID: `${article.system.id}/${article.system.language}`,
title: article.elements.title.value,
language: article.system.language,
postDate: new Date(article.elements.postDate.value).toString('dddd, MMMM d, yyyy'),
bodyCopy: resolveRichText(article.elements.bodyCopy),
summary: article.elements.summary.value,
teaserImage: article.elements.teaserImage.value[0].url,
type: article.system.type
});
});
res.redirect(`/${req.params.lang}`);
});
});
//routes
app.use('/', index);
app.use('/', cafes);
app.use('/', articles);
app.use('/', aboutUs);
app.use('/', contacts);
app.use('/', store);
app.use('/', coffee);
app.use('/', brewer);
app.use('/', search);
//register main.js for use in Pug
app.locals.formatPrice = formatPrice;
//catch 404 and forward to error handler
app.use(function(_req, _res, next) {
next(createError(404));
});
//error handler
app.use(function(err, req, res, _next) {
//set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
//render the error page
res.status(err.status || 500);
res.render('error');
});
export default app;