首页 > 解决方案 > Heroku NodeJS - 无法获取 /

问题描述

我有一个 Heroku 服务器,上面运行着BootBot 。但是我在尝试从中呈现 HTML 页面时遇到了困难。这就是我正在做的事情:

var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname)));
app.get('/test', function (req, res) {
    res.sendFile(path.join(__dirname + 'test.html'));
});
app.listen(3000, function() {
    console.log("The server is running.");
});

我的根目录有 index.js 和 test.html。

我的Procfile是: web: node --inspect ./index.js

我尝试过的一切都将我发送到相同的"Cannot GET /test".

有什么我想念的吗?

如果我设置app.listen(process.env.PORT)

State changed from starting to crashed
2018-11-22T09:48:29.702624+00:00 heroku[web.1]: Process exited with status 1
2018-11-22T09:48:29.606583+00:00 app[web.1]: The server is running.
2018-11-22T09:48:29.608553+00:00 app[web.1]: events.js:167
2018-11-22T09:48:29.608557+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2018-11-22T09:48:29.608558+00:00 app[web.1]:       ^
2018-11-22T09:48:29.608559+00:00 app[web.1]: 
2018-11-22T09:48:29.608561+00:00 app[web.1]: Error: listen EADDRINUSE :::20293
2018-11-22T09:48:29.608563+00:00 app[web.1]:     at Server.setupListenHandle [as _listen2] (net.js:1286:14)
2018-11-22T09:48:29.608564+00:00 app[web.1]:     at listenInCluster (net.js:1334:12)
2018-11-22T09:48:29.608565+00:00 app[web.1]:     at Server.listen (net.js:1421:7)
2018-11-22T09:48:29.608569+00:00 app[web.1]:     at Function.listen (/app/node_modules/express/lib/application.js:618:24)
2018-11-22T09:48:29.608570+00:00 app[web.1]:     at BootBot.start (/app/node_modules/bootbot/lib/BootBot.js:46:28)
2018-11-22T09:48:29.608572+00:00 app[web.1]:     at Object.<anonymous> (/app/index.js:550:5)
2018-11-22T09:48:29.608573+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:688:30)
2018-11-22T09:48:29.608574+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
2018-11-22T09:48:29.608576+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:598:32)
2018-11-22T09:48:29.608577+00:00 app[web.1]:     at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
2018-11-22T09:48:29.608579+00:00 app[web.1]: Emitted 'error' event at:
2018-11-22T09:48:29.608580+00:00 app[web.1]:     at emitErrorNT (net.js:1313:8)
2018-11-22T09:48:29.608581+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:63:19)
2018-11-22T09:48:29.608583+00:00 app[web.1]:     at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
2018-11-22T09:48:29.608584+00:00 app[web.1]:     at startup (internal/bootstrap/node.js:285:19)
2018-11-22T09:48:29.608585+00:00 app[web.1]:     at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

标签: javascripthtmlnode.jsexpress

解决方案


您需要模板引擎ejs,或者当您想blade从 node.Js 呈现 html 页面时。

npm install ejs --save

这些行到您的 index.js 文件

// ~ render html files with ejs 
app.use(express.static(path.join(__dirname))); 
app.set('views', __dirname);
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);

app.get('/test', function (req, res, next) {
    res.render('test.html');
});

我希望这可以帮助你。如果它没有通过评论传达给我。


推荐阅读