This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
58 lines (49 loc) · 1.79 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
const express = require('express')
const path = require('path')
const next = require('next')
const i18nextMiddleware = require('i18next-express-middleware')
const Backend = require('i18next-node-fs-backend')
const i18n = require('./i18n')
const routes = require('./routes')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handler = routes.getRequestHandler(app)
// init i18next with serverside settings
// using i18next-express-middleware
i18n
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
fallbackLng: 'en-US',
preload: ['en-US', 'ja-JP'], // preload all langages
ns: ['common'], // need to preload all the namespaces
backend: {
loadPath: path.join(__dirname, '/locales/{{lng}}/{{ns}}.json'),
addPath: path.join(__dirname, '/locales/{{lng}}/{{ns}}.missing.json')
}
}, () => {
// loaded translations we can bootstrap our routes
app.prepare()
.then(() => {
const server = express()
// mercari tech conf 2017
server.use('/2017', express.static(path.join(__dirname+'/static/2017')));
// enable middleware for i18next
server.use(i18nextMiddleware.handle(i18n))
// serve locales for client
server.use('/locales', express.static(path.join(__dirname, '/locales')))
// missing keys
server.post('/locales/add/:lng/:ns', i18nextMiddleware.missingKeyHandler(i18n))
// redirect root access to /2018
// server.get('/', (req, res) => res.redirect('/2018'))
// use next.js
// server.get('*', (req, res) => handle(req, res))
server.use(handler)
server.listen(8080, (err) => {
if (err) {
throw err
}
console.log('> Ready on http://localhost:8080')
})
})
})