简体   繁体   中英

How do I deploy node app to heroku without using Express

I deployed my node.js app successfully to heroku however when I accessed the site there was an error saying "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch"

my code is

var router = require("./router.js");

// Create a web server
var http = require("http");

http.createServer(function (request, response) {
  router.home(request, response);
  router.user(request, response);
}).listen(1337);
console.log("Sever running at http://127.0.0.1:1337/");

I figured it has something to do with the port. There was a post similar to my problem Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch) but their solutions were based on Express. I didn't use express for my app. Is there any other way to not set the port to a fixed number?

It doesn't matter if you use express or not. Herouku binds a port for you in the process.env.PORT environment variable. You need to use that like so:

var port = process.env.PORT || 1337

Then use .listen(port)
Make sure you're connecting to the correct port in your browser (log the port to console to be sure)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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