简体   繁体   English

Heroku NodeJS-无法获取/

[英]Heroku NodeJS - Cannot GET /

I have a Heroku server with BootBot running over it. 我有一运行BootBot的Heroku服务器。 But I'm having difficulties trying to render a HTML page from it. 但是我很难从中渲染HTML页面。 This is what I'm doing: 这就是我在做什么:

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.");
});

My root directory has both index.js and test.html. 我的根目录同时包含index.js和test.html。

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

Everything I've tried sends me to the same "Cannot GET /test" . 我尝试过的所有内容都将我发送到相同的"Cannot GET /test"

Is there something I'm missing? 有什么我想念的吗?

If I set app.listen(process.env.PORT) 如果我设置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)

You need template engine like ejs or blade when you want to render html page from node.Js. 要从node.Js渲染html页面时,需要ejsblade类的模板引擎。

npm install ejs --save

And these lines to your index.js file 这些行到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');
});

I hope this helps you. 我希望这可以帮助你。 if it does not reach me with the comments. 如果没有收到我的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM