|
@@ -24,29 +24,42 @@ app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
app.use(cookieParser());
|
|
app.use(cookieParser());
|
|
|
app.use(basePath, express.static(path.join(__dirname, 'public')));
|
|
app.use(basePath, express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
|
|
-app.use('/', index);
|
|
|
|
|
|
|
+// handle language
|
|
|
|
|
+var baseRoute = basePath + ':lang(' + Object.keys(strings).join('|') + ')?';
|
|
|
|
|
+app.use(baseRoute, function(req, res, next) {
|
|
|
|
|
+ res.locals.lang = req.params.lang || 'zh-cn';
|
|
|
|
|
+ res.locals.langs = Object.fromEntries(Object.keys(strings).map(function(lang) {
|
|
|
|
|
+ return [lang, new Intl.DisplayNames([lang], { type: 'language' }).of(lang)];
|
|
|
|
|
+ }));
|
|
|
|
|
+ res.locals.strings = strings[res.locals.lang];
|
|
|
|
|
+ res.locals.base_path = basePath;
|
|
|
|
|
+ next();
|
|
|
|
|
+});
|
|
|
|
|
+app.use(baseRoute, index);
|
|
|
|
|
+
|
|
|
|
|
+// apis
|
|
|
app.use(basePath + 'api', apis);
|
|
app.use(basePath + 'api', apis);
|
|
|
|
|
|
|
|
|
|
+// service worker
|
|
|
|
|
+app.get(basePath + 'sw.js', function(req, res, next) {
|
|
|
|
|
+ res.sendFile(path.join(__dirname, 'public/javascripts/sw.js'));
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
// catch 404 and forward to error handler
|
|
// catch 404 and forward to error handler
|
|
|
app.use(function(req, res, next) {
|
|
app.use(function(req, res, next) {
|
|
|
- var err = new Error('Not Found');
|
|
|
|
|
- err.status = 404;
|
|
|
|
|
- next(err);
|
|
|
|
|
|
|
+ var err = new Error('Not Found');
|
|
|
|
|
+ err.status = 404;
|
|
|
|
|
+ next(err);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
|
// error handler
|
|
|
app.use(function(err, req, res, next) {
|
|
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);
|
|
|
|
|
- var lang = (req.url.match(new RegExp('^' + basePath + '([^/]+)')) || [,])[1];
|
|
|
|
|
- if (!(lang in strings)) {
|
|
|
|
|
- lang = 'zh-cn';
|
|
|
|
|
- }
|
|
|
|
|
- res.render('error', { lang: lang, strings: strings[lang], base_path: basePath });
|
|
|
|
|
|
|
+ // set locals, only providing error in development
|
|
|
|
|
+ res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
|
+
|
|
|
|
|
+ // render the error page
|
|
|
|
|
+ res.status(err.status || 500);
|
|
|
|
|
+ res.render('error');
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|
|
module.exports = app;
|