我正在尝试将reveal.js应用程序部署到Heroku。 Reveal.js 通过
grunt connect
命令在节点上运行。该应用程序还需要 ruby 来动态编译资产。在本地,我可以使用 grunt serve
运行该应用程序。
最初,由于 compass 是
grunt watch
的依赖项,Heroku 仅检测到 Gemfile 并假设我正在运行 ruby 应用程序。我使用 NodeJS 自定义构建包来强制 Heroku 将其视为 NodeJS 应用程序。
Proc文件包含
web: grunt serve
日志显示
2013-06-17T13:51:56.187012+00:00 heroku[router]: at=error code=H14 desc="No web processes running"
heroku ps
也没有显示任何内容。我可以成功运行“heroku run gruntserve”,并且我已经修改了reveal附带的默认Gruntfile.js以接受process.env,即
connect: {
server: {
options: {
port: process.env.PORT || 8000,
base: '.'
}
}
}
作为最后一次尝试,我尝试使用 Heroku-nodejs-grunt 构建包(https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt),它将在部署时运行 grunt 任务来编译资产。仍然不走运,
heroku logs --tail
仍然显示没有进程正在运行。使用 heroku run
进行探索表明 grunt 可用,并且 grunt serve
命令成功执行。
当开始使用新的 grunt 构建包时,我收到上述 Gruntfile.js 的错误,提示“进程”未定义。我把端口改成0了。
网络服务器响应的端口。如果出现以下情况,任务将会失败 指定的端口已被使用。您可以使用特殊值 0 或者 '?'使用系统分配的端口。
没用,尝试了“?”,没用(仍然没有网络进程,
heroku restart
没有做任何事情)
我不知道如何让 Heroku 使用 grunt 作为我的主要 Web 服务器进程!
我能够使用nodejs和expressJs使其工作。
通过遵循heroku“nodejs入门”,我能够使用expressjs获得一个可以工作的web应用程序,并在web.js中声明它:
var express = require("express");
var app = express();
app.use(express.logger());
app.use("/", express.static(__dirname));
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
有了这个,您可以静态地提供所有内容。
您可以在这里找到资源:https://github.com/MichaelBitard/revealjs_heroku,还有一个工作示例:http://murmuring-cove-4212.herokuapp.com/
您的问题是默认情况下
grunt serve
绑定到 localhost
。为了使其正常工作,您需要对reveal.js
进行一些小更改:
首先添加
grunt-cli
作为 devDependency
:
diff --git a/package.json b/package.json
index 10489bb..4c58442 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"grunt-contrib-connect": "~0.8.0",
"grunt-autoprefixer": "~1.0.1",
"grunt-zip": "~0.7.0",
+ "grunt-cli": "~0.1.13",
"grunt": "~0.4.0",
"node-sass": "~0.9.3"
},
然后向
hostname
添加一个 grunt
参数,该参数将用于绑定到 0.0.0.0
而不是 localhost
。
diff --git a/Gruntfile.js b/Gruntfile.js
index 3e67b9f..b2bfc47 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -1,5 +1,6 @@
/* global module:false */
module.exports = function(grunt) {
+ var hostname = grunt.option('hostname') || 'localhost';
var port = grunt.option('port') || 8000;
// Project configuration
grunt.initConfig({
@@ -94,6 +95,7 @@ module.exports = function(grunt) {
connect: {
server: {
options: {
+ hostname: hostname,
port: port,
base: '.',
livereload: true,
现在您可以创建一个包含以下内容的
Procfile
并将其部署到 Heroku:
web: npm install && node_modules/.bin/grunt serve --hostname 0.0.0.0 --port $PORT
我已经为reveal.js
所需的更改创建了
PR。
目前 Express v~4.13.3
express.logger()
已弃用,不包含在 Express 包中。为了解决这个问题,我必须导入依赖项morgan
。
我的
web.js
文件最终如下:
var express = require('express');
var morgan = require('morgan');
var app = express();
var port = process.env.PORT || 5000;
app.use(morgan('combined'));
app.use('/', express.static(__dirname));
app.listen(port, function() {
console.log('Server started on ' + port);
});
此外,我需要更新我的 package.json 以包含 morgan 库。我在文件中的依赖项适用于:
...
"dependencies": {
"express": "~4.13.3",
"morgan": "~1.7.0",
"grunt-cli": "~0.1.13",
"mustache": "~2.2.1",
"socket.io": "~1.3.7"
},
...