0

I have a Heroku server with BootBot running over it. But I'm having difficulties trying to render a HTML page from it. 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.

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

Everything I've tried sends me to the same "Cannot GET /test".

Is there something I'm missing?

If I set 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)
Macaret
  • 797
  • 9
  • 33

1 Answers1

0

You need template engine like ejs or blade when you want to render html page from node.Js.

npm install ejs --save

And these lines to your index.js file

// ~ 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.

bereket gebredingle
  • 12,064
  • 3
  • 36
  • 47