-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (76 loc) · 2.84 KB
/
server.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
// Polyfill Node with `Intl` that has data for all locales.
// See: https://formatjs.io/guides/runtime-environments/#server
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
const express = require('express');
const session = require('express-session');
const {readFileSync} = require('fs');
const {basename} = require('path');
const {createServer} = require('http');
const accepts = require('accepts');
const glob = require('glob');
const next = require('next');
const config = require('./config');
// Load environment variable
require('dotenv').config();
const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();
// Get the supported languages by looking for translations in the `lang/` dir.
const languages = glob.sync('./lang/*.json').map((f) => basename(f, '.json'))
// We need to expose React Intl's locale data on the request for the user's
// locale. This function will also cache the scripts by lang in memory.
const localeDataCache = new Map();
const getLocaleDataScript = (locale => {
const lang = locale.split('-')[0];
if(!localeDataCache.has(lang)) {
const localeDataFile = require.resolve(`react-intl/locale-data/${lang}`);
const localeDataScript = readFileSync(localeDataFile, 'utf-8');
localeDataCache.set(lang, localeDataScript);
}
return localeDataCache.get(lang);
});
// We need to load and expose the translations on the request for the user's
// locale. These will only be used in production, in dev the `defaultMessage` in
// each message description in the source code will be used.
const getMessages = (locale) => {
return require(`./lang/${locale}.json`);
};
app.prepare().then(() => {
const server = express();
server.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}));
server.get('/:lg(\[a-z]{2}\)', (req, res) => {
switch (req.params.lg) {
case 'fr':
req.session.lang = 'fr';
break;
default:
req.session.lang = 'en';
}
res.redirect('/');
});
server.get('*', (req, res) => {
const accept = accepts(req);
const locale = accept.language(dev ? ['en'] : languages);
req.locale = locale;
req.localeDataScript = getLocaleDataScript(req.session.lang ? req.session.lang : locale);
req.messages = dev ? {} : getMessages(req.session.lang ? req.session.lang : locale);
handle(req, res);
});
server.listen(config.port, (err) => {
if(err){
throw err;
} else {
console.log('> Running on http://localhost:' + config.port)
}
})
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});