语言更改功能不起作用

问题描述 投票:0回答:1

我还制作了一个网站并在那里添加了语言更改功能。单词和文本的翻译位于 ruTrans.json、enTrance.json 等类型的文件中。这个功能可以在我笔记本电脑上的本地服务器上运行,但是当我将项目上传到 ispmanager 托管后,当您访问该网站并尝试更改语言时,它会发生变化,但网站立即更新并且语言再次变为俄语。

我在网上看到问题可能是缓存。我把它关掉了,但它仍然不起作用。服务器是用node js编写的。

const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');

const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url, true);

    if (parsedUrl.pathname === '/') {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/html;charset=utf-8');
        const data = fs.readFileSync(process.env.INDEX_PATH, 'utf8');
        res.end(data);
    } else if (parsedUrl.pathname.startsWith('/translates/') && parsedUrl.pathname.endsWith('.json')) {
        res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
        res.setHeader('Pragma', 'no-cache');
        res.setHeader('Expires', '0');

        const filePath = path.join(__dirname, parsedUrl.pathname.substring(1));
        if (fs.existsSync(filePath)) {
            const jsonData = fs.readFileSync(filePath, 'utf8');
            res.statusCode = 200;
            res.setHeader('Content-Type', 'application/json;charset=utf-8');
            res.end(jsonData);
        } else {
            res.statusCode = 404;
            res.setHeader('Content-Type', 'text/plain;charset=utf-8');
            res.end('Not Found');
        }
    } else {
    
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/plain;charset=utf-8');
        res.end('Not Found');
    }
});

if ("SOCKET" in process.env) {
    const socket = process.env.SOCKET;

    if (fs.existsSync(socket)) {
        fs.unlinkSync(socket);
    }
    server.listen(socket, () => {
        fs.chmodSync(socket,0660);
        console.log(`Listening ${socket}`);
    });
} else if ("PORT" in process.env) {
    const hostname = process.env.INSTANCE_HOST;
    const port = process.env.PORT;
    server.listen(port, hostname, () => {
        console.log(`Listening http://${hostname}:${port}/`);
    });
}
javascript html node.js json hosting
1个回答
0
投票

您应该使用浏览器的开发工具/网络选项卡来检查网站的加载情况。

更具体地说,您应该确定用户更改语言后再次加载页面的最后一个请求。然后,点击请求,进入“发起者”选项卡,您可以找到哪一段代码/实体发送了请求。

© www.soinside.com 2019 - 2024. All rights reserved.