gpt4 book ai didi

javascript - 如何修复 Heroku 中的 window.open() 问题?

转载 作者:行者123 更新时间:2023-12-03 01:08:29 26 4
gpt4 key购买 nike

我正在尝试使用 Node.js 在 Heroku 上托管一个网页。是的,我知道 php 方法,但我想用 Node 来代替。实际上到目前为止它运行得很好,只有一个小问题。

我的Procfile如下:web: Node main.js

我的 main.js 文件:window.open("index.html", "_self");

我的 index.html 文件很好并连接到它的其余部分,但它很长,所以我真的不想将其发布在这里。

我有一个 package.json 等,但当我尝试部署应用程序时,会弹出以下错误。

State changed from crashed to starting
2018-09-12T03:23:22.000000+00:00 app[api]: Build succeeded
2018-09-12T03:23:24.424276+00:00 heroku[web.1]: Starting process with command `node main.js`
2018-09-12T03:23:26.694405+00:00 heroku[web.1]: Process exited with status 1
2018-09-12T03:23:26.710365+00:00 heroku[web.1]: State changed from starting to crashed
2018-09-12T03:23:26.630643+00:00 app[web.1]: /app/main.js:1
2018-09-12T03:23:26.630664+00:00 app[web.1]: (function (exports, require, module, __filename, __dirname) { window.open("index.html", "_self");
2018-09-12T03:23:26.630666+00:00 app[web.1]: ^
2018-09-12T03:23:26.630668+00:00 app[web.1]:
2018-09-12T03:23:26.630670+00:00 app[web.1]: ReferenceError: window is not defined
2018-09-12T03:23:26.630671+00:00 app[web.1]: at Object.<anonymous> (/app/main.js:1:63)
2018-09-12T03:23:26.630672+00:00 app[web.1]: at Module._compile (module.js:653:30)
2018-09-12T03:23:26.630674+00:00 app[web.1]: at Object.Module._extensions..js (module.js:664:10)
2018-09-12T03:23:26.630676+00:00 app[web.1]: at Module.load (module.js:566:32)
2018-09-12T03:23:26.630677+00:00 app[web.1]: at tryModuleLoad (module.js:506:12)
2018-09-12T03:23:26.630679+00:00 app[web.1]: at Function.Module._load (module.js:498:3)
2018-09-12T03:23:26.630680+00:00 app[web.1]: at Function.Module.runMain (module.js:694:10)
2018-09-12T03:23:26.630682+00:00 app[web.1]: at startup (bootstrap_node.js:204:16)
2018-09-12T03:23:26.630684+00:00 app[web.1]: at bootstrap_node.js:625:3

显然,这意味着它无法识别“窗口”,但我不知道如何解决这个问题。如果有人有答案,我将非常感激。谢谢!

最佳答案

在 Heroku 上运行时,您在浏览器中使用 NodeJS,而不是 Javascript。 Node.js 中没有窗口。所以你不能使用window.open

var http = require('http');
var fs = require('fs');

var html = fs.readFileSync('index.html');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}).listen(process.env.PORT || 5000);

这将在端口 $PORT 或 5000 上启动一个 Web 服务器,并为所有请求提供 index.html 文件。它应该在 Heroku 上按原样工作。

关于javascript - 如何修复 Heroku 中的 window.open() 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52287132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com