我有一个使用vanilla和node js构建的简单静态网站。
它可以在生产环境中找到,但是在我实现了全球化和本地化功能之后,在本地主机上抛出了不正确的文件路径错误。
“ message”:“ ENOENT:没有这样的文件或目录,统计 '/Users/jiahlee/Documents/portfolio/production/server/views/zh-CN/index.html'“,
应搜索src目录,而不是如下所示的生产目录。
/Users/jiahlee/Documents/portfolio/src/server/views/zh-CN/index.html
app.get('env')获得'development'值并运行代码的正确部分。但它不会运行app.get()。
在index.js中,呈现了“它处于开发模式”控制台,但其余部分未呈现。
const SUPPORTED_LANGUAGES = {
en: 1,
ko: 1
};
app.get('/', (req, res, next) => {
req._lang = req.query.lang || req.cookies.lang;
if (SUPPORTED_LANGUAGES[req._lang]) {
// Set the language in the cookie for user accessing via heroku
res.cookie('lang', req._lang);
} else {
req._lang = 'en';
}
next();
});
if (app.get('env') === 'production') {
app.get('/', (req, res) => {
res.redirect(`https://****.github.io/${req._lang}/`);
});
app.get('/en', (req, res) => {
res.redirect('https://****.github.io/en/');
});
app.get('/ko', (req, res) => {
res.redirect('https://****.github.io/ko/');
});
} else {
app.use('/vendors', express.static(`${process.cwd()}/vendors`));
app.use('/resources', express.static(path.join(__dirname, '../', 'client/resources')));
app.use(express.static(`${process.cwd()}/static`));
console.log('It\'s on Dev mode');
app.get('/', (req, res) => {
console.log('########1');
res.sendFile(path.join(__dirname, `/views/${req._lang}/index.html`));
});
app.get('/ko', function (req, res) {
console.log('########2');
res.sendFile(path.join(__dirname, '/views/ko/index.html'));
});
app.get('/en', function (req, res) {
console.log('########3');
res.sendFile(path.join(__dirname, '/views/en/index.html'));
});
app.get('/index.html', function (req, res) {
console.log('########4');
res.sendFile(path.join(__dirname, '/views/index.html'));
});
}
/* --- Multi-languages ( Globalization & Localization ) --- */
function getCookie(name) {
var value = '; ' + document.cookie;
var parts = value.split('; ' + name + '=');
if (parts.length == 2) return parts.pop().split(';').shift();
}
function setCookie(name,value,days) {
var expires = '';
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + (value || '') + expires + '; path=/';
}
const activeLang = ({
'/en': 'en',
'/ko': 'ko'
})[location.pathname.replace(/\/$/, '')] || 'en';
const browserLang = (navigator.language || navigator.userLanguage).split('-')[0];
const cookieLanguage = getCookie('lang');
// Browser language is different than the set language and the url
// querystring does not have a lang parameter
// set cookie for user accessing via github.
if (browserLang && !cookieLanguage) {
// set cookie if not exist.
setCookie('lang', browserLang, 365);
return window.location = '/' + browserLang;
}
// re-set cookie when cookie already exists.
setCookie('lang', activeLang, 365);
// The main index.html is in English
if (location.pathname === '/' && cookieLanguage !== 'en') {
return window.location = '/' + cookieLanguage;
}
<head>
<link rel="canonical" href="https://****.github.io/index.html" />
<link rel="alternate" href="https://****.github.io/ko/" hreflang="ko" />
<link rel="alternate" href="https://****.github.io/en/" hreflang="en" />
</head>
<body>
<script src="/resources/js/app.js" async></script>
</body>