从 运行ning npm start + 什么到 运行 停止 Heroku?

Stopping Heroku from running npm start + what to run instead?

免责声明:我是节点。js/grunt/bower新手。

我有一个 node.js/grunt/bower 应用程序,我正尝试在 Heroku 上部署它。 Heroku 按预期构建应用程序,但随后它尝试 运行 "npm start" 我没有在 package.json 文件中指定。此命令失败,我猜是因为它不是服务器应用程序,而且我看不到部署在 Heroku 上的实际应用程序。我的申请可以在这里找到:https://github.com/uzilan/sufusku

那么 - 我如何让 Heroku 不 运行 "npm start" 命令,而应该 运行 什么? 运行ning g运行t 构建后,应用程序在 dist 文件夹中可用。

package.json:

{
    "name": "sufusku",
    "version": "0.0.1",
    "description": "Sufusku - makes your annoying sudoku easier",
    "repository": {
        "type": "git",
        "url": "git://github.com/uzilan/sufusku.git"
    },
    "license": "(MIT OR Apache-2.0)",
    "dependencies": {
        "bower": "^1.4.1",
        "grunt": "0.4.5",
        "grunt-cli": "0.1.13",
        "grunt-contrib-clean": "^0.6.0",
        "grunt-contrib-concat": "^0.5.1",
        "grunt-contrib-connect": "^0.11.2",
        "grunt-contrib-copy": "^0.8.0",
        "grunt-contrib-cssmin": "^0.13.0",
        "grunt-contrib-htmlmin": "^0.4.0",
        "grunt-contrib-uglify": "^0.9.1",
        "grunt-contrib-watch": "^0.6.1",
        "grunt-ng-annotate": "^1.0.1",
        "load-grunt-config": "^0.17.2",
        "time-grunt": "^1.2.1"
    },
    "devDependencies": {},
    "engines": {
        "node": ">=0.8.0"
    },
    "scripts": {
        "postinstall": "./node_modules/bower/bin/bower install && grunt build"
    }
}

Gruntfile.js:

module.exports = function (grunt) {
    var appname = require('./package.json').name;

    require('load-grunt-config')(grunt, {

        data: {
            dist: 'dist/' + appname,
            app: 'src',
            distscripts: 'dist/' + appname + '/scripts',
            diststyles: 'dist/' + appname + '/styles',
            disthtml: 'dist/' + appname,
            disttest: 'dist/' + appname + '/test',
            appstyles: 'src/css'
        }
    });

    require('time-grunt')(grunt);

    grunt.registerTask('build', ['clean:dist', 'ngAnnotate:application', 'uglify', 'htmlmin', 'cssmin', 'copy:serve', 'concat:components']);

    grunt.registerTask('serve', ['build', 'connect:livereload', 'watch']);

    grunt.registerTask('default', function () {
        grunt.task.run(['build']);
    });
};

Heroku 日志:

...
heroku[web.1]: Starting process with command `npm start`
app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
app[web.1]: npm ERR! Linux 3.13.0-57-generic
app[web.1]: npm ERR! npm  v2.11.3
app[web.1]: npm ERR! missing script: start
app[web.1]: npm ERR! Please include the following file with any support request: 
app[web.1]: npm ERR! 
app[web.1]: npm ERR! node v0.12.7
app[web.1]: 
app[web.1]: npm ERR!     /app/npm-debug.log
app[web.1]: npm ERR! If you need help, you may report this error at:
app[web.1]: 
app[web.1]: npm ERR!     <https://github.com/npm/npm/issues>
heroku[web.1]: Process exited with status 1
heroku[web.1]: State changed from starting to crashed
heroku[web.1]: State changed from crashed to starting
heroku[web.1]: Starting process with command `npm start`
app[web.1]: npm ERR! Linux 3.13.0-57-generic
app[web.1]: npm ERR! node v0.12.7
app[web.1]: 
app[web.1]: npm ERR! missing script: start
...
当 Procfile 中没有指定其他进程时,

npm start 是默认的 web 进程 运行。既然你说你不需要服务器(web 进程),那么首先要做的是将默认进程缩放为零:

heroku scale web=0

接下来,您需要通过将其进程类型添加到名为 Procfile 的文件来告诉 Heroku 您想要 运行 而不是 web 的进程。例如,如果您的应用以 node foobar.js 开头,您可以创建一个 Procfile,如下所示:

bot: node foobar.js

然后将 'bot' 进程扩展到至少一个:

heroku scale bot=1

查看上面的代码,即使您说它不是基于服务器的应用程序,它看起来也很像基于服务器的应用程序。你如何在本地启动你的应用程序?无论 Procfile 中应该包含什么答案,您都可以在此处了解更多信息:

披露:我是 HerokuNode.js 平台的所有者

感谢您的回答。在尝试上述教程并查看 node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON] 中的解决方案后,我最终编写了一个 server.js 文件来解决问题。它看起来像这样:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.get(/^(.+)$/, function (req, res) {
    res.sendFile(__dirname + req.params[0]);
});

var PORT = process.env.PORT || 3000;

app.listen(PORT);