Heroku NodeJS-无法获取/

时间:2018-11-22 09:05:54

标签: javascript html node.js express

我有一台运行BootBot的Heroku服务器。但是我很难从中渲染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)

1 个答案:

答案 0 :(得分:0)

当您要从node.Js呈现html页面时,您需要ejsblade之类的模板引擎

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');
});

希望这对您有所帮助。如果没有收到我的评论。