【问题标题】:Html Files cut off after hosting it on localhost server with Node.jsHtml 文件在使用 Node.js 托管在 localhost 服务器上后被切断
【发布时间】:2018-06-16 04:58:00
【问题描述】:

这是在我的 node.js 脚本文件中获取文件的代码。

app.get('/',function(req,res){
app.use(express.static(__dirname));
  res.sendFile(path.join(__dirname+'/Login.html'));
  res.sendFile(path.join(__dirname+'/View.html'));
    res.sendFile(path.join(__dirname+'/Setup.html'));
});

有一段时间我的代码运行良好,一切运行顺利。然后我的html文件开始被切断。例如 login.html 是我的 index html,它有 250 行,在 locahost 中运行后,很多按钮都坏了。我用 f11 检查了源代码,它只显示了 215 行。

这是一个例子: 记事本++:https://i.imgur.com/hyqi03t.png

本地主机:https://i.imgur.com/VI6eurr.png

此外,localhost 仅加载 6 个文件中的 4 个。刷新后,经常显示错误的页面为 index.html 或仅加载 6 个文件中的 1 个。 https://i.imgur.com/kHjR7XF.png

这个网站运行了一段时间,在我回滚了几个版本后,它甚至似乎无法正常运行。

我在 Console.log 中得到的错误是 Error: Can't set header after they're sent.

【问题讨论】:

    标签: javascript html node.js express


    【解决方案1】:

    您不应将当前文件夹 (__dirname) 设为静态,因为这可能会泄漏您的服务器脚本,而是将所有内容替换为:app.use(express.static('www')); 并将您的 html 文件放在一个名为 www 的新目录中。 Express 将自动提供文件,因此无需手动发送文件

    【讨论】:

    • 您好,谢谢您的回答,我是一个新的node.js用户,所以我不熟悉将文件上传到目录。我该怎么做?
    • 你在说什么??您的服务器是托管在 VPS 上还是什么的?然后使用提供的方法,例如 (s)FTP。您也可以使用终端命令(linux:mv file1 www/file1)移动您已经拥有的文件(windows:idk,您可能有一个 GUI)
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2017-10-20
    • 2016-12-29
    • 2018-11-16
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多