-
Notifications
You must be signed in to change notification settings - Fork 157
/
server.js
74 lines (61 loc) · 2.32 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
const express = require('express');
// const path = require('path');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dir: '.', dev });
const handle = app.getRequestHandler();
// const { createServer } = require('http');
const i18nextMiddleware = require('i18next-http-middleware');
const Backend = require('i18next-fs-backend');
const i18n = require('./i18n');
// const compression = require('compression');
const Resources = require('./locales/index.js');
const languageList = require('./lists/languages').languages;
// init i18next with serverside settings
// using i18next-express-middleware
i18n
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
preload: languageList,
load: 'languageOnly', // no region specific locals like en-US, de-DE
resources: Resources,
// need to preload all the namespaces
// add more namespaces as pages are created
ns: ['common', 'header', 'footer', 'home', 'presskit', 'pressreleases', 'meetup', 'meetup-2018', 'meetup-2019'],
backend: {
loadPath: `${__dirname}/locales/{{lng}}/{{ns}}.json`,
addPath: `${__dirname}/locales/{{lng}}/{{ns}}.missing.json`,
},
}, () => {
// loaded translations we can bootstrap our routes
app.prepare()
.then(() => {
const e = express();
// compression middleware - Everything below will be compressed
// e.use(compression());
// enable middleware for i18next
e.use(i18nextMiddleware.handle(i18n));
// serve locales for client
e.use('/locales', express.static(`${__dirname}/locales`));
// missing keys
e.post('/locales/add/:lng/:ns', i18nextMiddleware.missingKeyHandler(i18n));
// use next.js
e.get('*', (req, res) => handle(req, res));
e.listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
// const s = createServer((req, res) => {
// if (req.url === '/sw.js') {
// app.serveStatic(req, res, path.resolve('./static/sw.js'));
// } else {
// handle(req, res);
// }
// });
// s.listen(3000, (err) => {
// if (err) throw err;
// console.log('> Ready on http://localhost:3000');
// });
});
});