1 const express = require('express'); 2 const i18n = require('i18n'); 3 var path = require('path'); 4 var app = express(); 5 6 var server = app.listen(3000, function(){ 7 var host = server.address().address; 8 var port = server.address().port; 9 10 console.log('run', host, port); 11 }); 12 13 //中间件 14 var setI18n = () => { 15 const i18nOptions = { 16 locales: ['en', 'zh-cn', 'zh-tw'], 17 directory: path.join(__dirname, './src/locales/server'), 18 objectNotation: true, 19 updateFiles: true, 20 }; 21 22 i18n.configure(i18nOptions); 23 return i18n.init; 24 }; 25 26 const setLang = (req, res, next) => { 27 res.setLocale('zh-tw'); 28 next(); 29 }; 30 31 app.use(setI18n()); 32 app.use(setLang); 33 34 //项目文件中的pug模版 35 app.set('views', [ 36 path.join('./src/', '/views/') 37 ]); 38 app.set('./src', 'pug'); 39 40 //渲染模版 41 app.get('/',function(req, res){ 42 res.render('partials/nav.pug', { 43 config: { 44 APP_NAME: 'books' 45 } 46 }); 47 });