简体   繁体   English

在 heroku 上部署 node.js 应用程序成功但不起作用

[英]Deploy node.js app on heroku succeeds but doesn't work

I have a very simple, straight-forward node.js app [1] I want to deploy on heroku.我有一个非常简单、直接的 node.js 应用程序 [1] 我想在 heroku 上部署。 Although I'm able to deploy it, the page can't be accessed in the browser.虽然我能够部署它,但无法在浏览器中访问该页面。

I followed the advices from the 'Getting Started with Node.js on Heroku' guide [2].我遵循了“Heroku 上的 Node.js 入门”指南 [2] 中的建议。 When I run the application using node index.js locally, I'm able to access the application at http://localhost:8080/index.jade , however, when I try to access it on heroku at http://immonow.herokuapp.com:8080/index.jade it would throw me a ERR_CONNECTION_REFUSED HTTP error code.当我在本地使用node index.js运行应用程序时,我可以通过http://localhost:8080/index.jade访问该应用程序,但是,当我尝试在http://immonow.herokuapp.com:8080/index.jade heroku 上访问它时http://immonow.herokuapp.com:8080/index.jade它会给我一个ERR_CONNECTION_REFUSED HTTP 错误代码。

How I deployed my app:我如何部署我的应用程序:

  1. git commit -am "made changes" // commit changes git commit -am "made changes" // 提交更改
  2. git push origin master // push to git git push origin master // 推送到 git
  3. heroku create // create heroku app heroku create // 创建 heroku 应用程序
  4. git push heroku master // push to heroku git push heroku master // 推送到heroku
  5. heroku ps:scale web=1 // start workers heroku ps:scale web=1 // 开始工作

My node.js server:我的 node.js 服务器:

#!/usr/bin/env node
var http = require('http')
  , jade = require('jade')
  , static = require('node-static')
  , jadeRe = /\.jade$/
  , path = process.argv.slice(2)[0]
  , fileServer = new static.Server(path || '.')

http.createServer(function (req, res) {
  if (req.url.match(jadeRe)) {
    res.writeHead(200, {'Content-Type': 'text/html'})
    res.end(jade.renderFile('.' + req.url, {
      filename: '.' + req.url.replace(jadeRe, '')
    }))
  } else {
    req.addListener('end', function () {
      fileServer.serve(req, res)
    }).resume()
  }
}).listen(8080)

Any help would be appreciated.任何帮助,将不胜感激。

[1] https://github.com/takahser/immonow [1] https://github.com/takahser/immonow

[2] https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction [2] https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

Since I was not able to get it to work using the http package, I decided to use express instead.由于我无法使用 http 包使其工作,因此我决定改用 express。 As for the port, I had to do it as follow至于端口,我不得不这样做

var port = process.env.PORT || 3000;
app.listen(port);

in order to get it to work [1].为了让它工作[1]。

Here is my full working server:这是我的完整工作服务器:

/**
 * Module dependencies.
 */
var express = require('express');

// Path to our public directory
var pub = __dirname + '/public';

// setup middleware
var app = express();
app.use(express.static(pub));
app.use("/css", express.static(__dirname + '/css'));
app.use("/font", express.static(__dirname + '/font'));
app.use("/img", express.static(__dirname + '/img'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/video", express.static(__dirname + '/video'));

// Set our default template engine to "jade"
// which prevents the need for extensions
// (although you can still mix and match)
app.set('view engine', 'jade');

app.get('/', function(req, res){
  res.render('index');
});

app.get('/*', function(req, res){
  console.log(req.url.replace("/",""));
  res.render(req.url.replace("/",""));
});

// change this to a better error handler in your code
// sending stacktrace to users in production is not good
app.use(function(err, req, res, next) {
  res.send(err.stack);
});

/* istanbul ignore next */
if (!module.parent) {
  var port = process.env.PORT || 3000;
  app.listen(port);
  console.log('Express started on port 3000');
}

[1] see: Node.js port issue on Heroku cedar stack [1] 参见: Heroku cedar 堆栈上的 Node.js 端口问题

Things I had to do..我不得不做的事情..

  1. Create a Procfile in the root directory (a file literally called Procfile, no extension).在根目录中创建一个 Procfile(一个字面称为 Procfile 的文件,没有扩展名)。
    Inside Procfile, type:在 Procfile 中,键入:

    web: node server.js

  2. Add a script and engine to package.json将脚本和引擎添加到 package.json

    "scripts": { "start": "node server.js" }

    "engines": { "node": "8.9.3" }

  3. Update port in server.js在 server.js 中更新端口

    var port = process.env.PORT || 8080;

And why..还有为什么..

  1. To explicitly declare what command should be executed to start your app.明确声明应该执行什么命令来启动您的应用程序。 Heroku looks for a Procfile specifying your process types Heroku 寻找一个 Procfile 来指定你的进程类型

  2. For the script, if no Procfile is present in the root directory during the build process, your web process will be started by running npm start.对于脚本,如果在构建过程中根目录中不存在 Procfile,则您的 web 进程将通过运行 npm start 来启动。 For the engine, to specify a Node version that matches the runtime you're developing with and want to use on Heroku.对于引擎,指定一个与您正在开发的运行时相匹配并希望在 Heroku 上使用的 Node 版本。

  3. Heroku already assigns your app a port and adds it to the env, so you can't set the port to a fixed number. Heroku 已经为您的应用程序分配了一个端口并将其添加到环境中,因此您无法将端口设置为固定数字。

Resources:资源:

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

https://devcenter.heroku.com/articles/troubleshooting-node-deploys https://devcenter.heroku.com/articles/troubleshooting-node-deploys

https://devcenter.heroku.com/articles/deploying-nodejs https://devcenter.heroku.com/articles/deploying-nodejs

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

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