-
Notifications
You must be signed in to change notification settings - Fork 73
/
server.js
90 lines (76 loc) · 2.35 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
/* eslint-disable @typescript-eslint/no-var-requires */
const express = require('express');
const next = require('next');
const cookieParser = require('cookie-parser');
const pino = require('pino-http')();
const app = next({ dev: false });
const handle = app.getRequestHandler();
const PORT = process.env.PORT || 3000;
const DEFAULT_LOCALE = 'zh';
const FALLBACK_LOCALE = 'en';
const SUPPORTED_LOCALES = ['zh', 'en'];
const isStaticRoute = (url) => url.startsWith('/_next/data/');
const getLocaleFromUrl = (path) => {
const urlParts = path.split('/');
return SUPPORTED_LOCALES.includes(urlParts[1]) ? urlParts[1] : null;
};
const determineLocale = (req, urlLocale) => {
let locale = req.cookies.locale;
if (!locale) {
const browserLocale = req.acceptsLanguages(SUPPORTED_LOCALES);
locale = urlLocale || browserLocale || FALLBACK_LOCALE;
if (browserLocale && browserLocale !== 'zh') {
locale = FALLBACK_LOCALE;
}
}
return SUPPORTED_LOCALES.includes(locale) ? locale : FALLBACK_LOCALE;
};
const handleRedirect = (req, res, locale, urlLocale) => {
if (urlLocale) {
if (urlLocale !== locale) {
locale = urlLocale;
}
if (locale === DEFAULT_LOCALE) {
const newUrl = req.url.replace(`/${DEFAULT_LOCALE}`, '') || '/';
res.redirect(307, newUrl);
return true;
}
} else if (locale !== DEFAULT_LOCALE) {
let newUrl = `/${locale}${req.url}`;
if (newUrl.endsWith('/') && newUrl.length > 1) {
newUrl = newUrl.slice(0, -1);
}
res.redirect(307, newUrl);
return true;
}
return false;
};
async function startServer() {
try {
await app.prepare();
const server = express();
server.use(cookieParser());
server.use(pino);
server.use((req, res, next) => {
if (isStaticRoute(req.url)) {
return next();
}
const urlLocale = getLocaleFromUrl(req.path);
let locale = determineLocale(req, urlLocale);
if (handleRedirect(req, res, locale, urlLocale)) {
return; // 如果发生重定向,立即返回
}
req.locale = locale;
next();
});
server.all('*', (req, res) => handle(req, res));
server.listen(PORT, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${PORT}`);
});
} catch (error) {
console.error('Error starting server:', error);
process.exit(1);
}
}
startServer();