【问题标题】:Heroku needs a server.js file. What to put in it?Heroku 需要一个 server.js 文件。里面放什么?
【发布时间】:2016-04-01 15:49:34
【问题描述】:

我正在开发一个简单的节点应用程序,并且在本地运行良好。但是在 Heroku 上部署时,我遇到了一个错误。 Heroku 上的教程告诉添加 package.json

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

但是,我可以在 server.js 中添加什么?我发现了很多例子,但对我来说没有什么清楚或相关的。我对这个需求有点困惑。

应用程序的设置很常见:npm install/bower install 然后“grunt serve”启动应用程序。该应用是一个简单的 HTML/Angular Web 应用,在 API 上调用端点。

注意:我没有使用 Express。

这里是 gruntfile.js:

'use strict';

module.exports = function (grunt) {

    // Load grunt tasks automatically
    require('load-grunt-tasks')(grunt);

    // Time how long tasks take. Can help when optimizing build times
    require('time-grunt')(grunt);

    // Configurable paths for the application
    var appConfig = {
        app: require('./bower.json').appPath || 'app',
        dist: '../project/WebApp'
    };


    //Environment vars
    var envConfig = {
        dev: {
            baseUrl: 'http://localhost:3000',
            loginUrl: 'http://demo.lvh.me:9000'
        },
        prod: {
            baseUrl: '',
            loginUrl: ''
        }
    };

    // Require ModRewrite object before initConfig
    var modRewrite = require('connect-modrewrite');

    // Define the configuration for all the tasks
    grunt.initConfig({

        appConf: appConfig,

        // Empties folders to start fresh
        clean: {
            dist: {
                files: [{
                    dot: true,
                    src: [
                        '.tmp',
                        '<%= appConf.dist %>/{,*/}*',
                        '!<%= appConf.dist %>/.git*'
                    ]
                }],
                options: {
                    force: true //since dist directory is outside, we must use force flag.
                }
            },
            server: '.tmp'
        },

        //server settings
        ngconstant: {
            // Options for all targets
            options: {
                space: '  ',
                wrap: '\'use strict\';\n\n {%= __ngModule %}',
                name: 'config'
            },
            // Environment targets
            development: {
                options: {
                    dest: 'app/config.js'
                },  
                constants: envConfig.dev
            },
            production: {
                options: {
                    dest: '<%= appConf.app %>/config.js'
                },
                constants: envConfig.prod
            }
        },

        // Automatically inject Bower components into the app
        wiredep: {
            app: {
                src: ['<%= appConf.app %>/index.html'],
                ignorePath: /\.\.\//,
                'options': {
                    'overrides': {
                        'semantic-ui': {
                            'main': ['dist/semantic.css', 'dist/semantic.js']
                        }
                    }
                }
            }
        },

        // Copies remaining files to places other tasks can use
        copy: {
            dist: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: '<%= appConf.app %>',
                    dest: '<%= appConf.dist %>',
                    src: [
                        '*.{ico,png,txt}',
                        '{,*/}*.html',
                        'images/{,*/}*.{webp}',
                        'fonts/*'
                    ]
                }, {
                    expand: true,
                    cwd: '.tmp/images',
                    dest: '<%= appConf.dist %>/images',
                    src: ['generated/*']
                }, {
                    expand: true,
                    cwd: 'bower_components/font-awesome',
                    src: 'fonts/*',
                    dest: '<%= appConf.dist %>'
                }]
            },
            styles: {
                files: [
                    {
                        expand: true,
                        cwd: '<%= appConf.app %>/styles',
                        dest: '.tmp/styles/',
                        src: '{,*/}*.css'
                    }
                ]
            }
        },

        // Run some tasks in parallel to speed up the build process
        concurrent: {
            server: [
                'copy:styles'
            ],
            test: [
                'copy:styles'
            ],
            dist: [
                'copy:styles',
                'svgmin'
            ]
        },

        // Add vendor prefixed styles
        autoprefixer: {
            options: {
                browsers: ['last 1 version']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: '.tmp/styles/',
                    src: '{,*/}*.css',
                    dest: '.tmp/styles/'
                }]
            }
        },

        // The actual grunt server settings
        connect: {
            options: {
                port: 9000,
                // Change this to '0.0.0.0' to access the server from outside.
                hostname: 'demo.lvh.me',
                livereload: 35729
            },
            livereload: {
                options: {
                    open: true,
                    middleware: function (connect) {
                        return [
                            modRewrite(['^[^\\.]*$ /index.html [L]']),
                            connect.static('.tmp'),
                            connect().use(
                                '/bower_components',
                                connect.static('./bower_components')
                            ),
                            connect.static(appConfig.app)
                        ];
                    }
                }
            },
            test: {
                options: {
                    port: 9001,
                    middleware: function (connect) {
                        return [
                            rewriteRulesSnippet,
                            connect.static('.tmp'),
                            connect.static('test'),
                            connect().use(
                                '/bower_components',
                                connect.static('./bower_components')
                            ),
                            connect.static(appConfig.app)
                        ];
                    }
                }
            },
            dist: {
                options: {
                    open: true,
                    base: '<%= appConf.dist %>'
                }
            }
        },

        // Make sure code styles are up to par and there are no obvious mistakes
        jshint: {
            options: {
                jshintrc: '.jshintrc',
                reporter: require('jshint-stylish')
            },
            all: {
                src: [
                    'Gruntfile.js',
                    '<%= appConf.app %>/{,*/}*.js'
                ]
            }
        },

        // Watches files for changes and runs tasks based on the changed files
        watch: {
            bower: {
                files: ['bower.json'],
                tasks: ['wiredep']
            },
            js: {
                files: ['<%= appConf.app %>/{,**/}*.js'],
                tasks: ['newer:jshint:all'],
                options: {
                    livereload: '<%= connect.options.livereload %>'
                }
            },
            css: {
                files: ['<%= appConf.app %>/styles/{,**/}*.scss'],
                tasks: ['sass'],
                options: {
                    livereload: true,
                },
            },
            styles: {
                files: [
                    '<%= appConf.app %>/styles/{,**/}*.css',
                    '<%= appConf.app %>/../templating/css/style.css'
                ],
                tasks: ['newer:copy:styles', 'autoprefixer']
            },
            gruntfile: {
                files: ['Gruntfile.js']
            },
            livereload: {
                options: {
                    livereload: '<%= connect.options.livereload %>'
                },
                files: [
                    '<%= appConf.app %>/{,**/}*.html',
                    '.tmp/styles/{,**/}*.css',
                    '<%= appConf.app %>/images/{,**/}*.{png,jpg,jpeg,gif,webp,svg}'
                ]
            }
        },

        sass: {
            dev: {
                options: {
                    style: 'expanded'
                },
                files: [
                    {
                        expand: true,
                        cwd: '<%= appConf.app %>/styles',
                        src: ['*.scss'],
                        ext: '.css',
                        dest: '<%= appConf.app %>/styles'
                    }
                ]
            }
        }

    });

    grunt.loadNpmTasks('grunt-sass');

    // Build the development application
    grunt.registerTask('serve', 'Compile then start a connect web server', function () {

        grunt.task.run([
            'clean:server',
            'ngconstant:development',
            'wiredep',
            'sass',
            'concurrent:server',
            'autoprefixer',
            'connect:livereload',
            'watch'
        ]);
    });
};

【问题讨论】:

    标签: node.js heroku


    【解决方案1】:

    本教程假设 server.js 包含您的实际服务器。

    如果您的服务器是由grunt serve 启动的,只需更改start 脚本来运行它。

    【讨论】:

      【解决方案2】:

      这对我来说似乎更合乎逻辑 :) 所以我对其进行了测试并且部署良好,但应用程序因此错误而崩溃:

      Starting process with command npm start
      2016-03-03T17:14:35.928347+00:00 app[web.1]: 
      2016-03-03T17:14:35.928397+00:00 app[web.1]: > @ start /app
      2016-03-03T17:14:35.928412+00:00 app[web.1]: > grunt serve
      2016-03-03T17:14:35.928413+00:00 app[web.1]: 
      2016-03-03T17:14:35.951390+00:00 app[web.1]: sh: 1: grunt: not found
      2016-03-03T17:14:35.963949+00:00 app[web.1]: 
      2016-03-03T17:14:35.978885+00:00 app[web.1]: npm ERR! Linux 3.13.0-77-generic
      2016-03-03T17:14:35.979392+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
      2016-03-03T17:14:35.979644+00:00 app[web.1]: npm ERR! node v4.2.2
      2016-03-03T17:14:35.984767+00:00 app[web.1]: npm ERR! npm  v2.14.7
      2016-03-03T17:14:35.985031+00:00 app[web.1]: npm ERR! file sh
      2016-03-03T17:14:35.985243+00:00 app[web.1]: npm ERR! code ELIFECYCLE
      2016-03-03T17:14:35.985481+00:00 app[web.1]: npm ERR! errno ENOENT
      2016-03-03T17:14:35.985718+00:00 app[web.1]: npm ERR! syscall spawn
      2016-03-03T17:14:35.985939+00:00 app[web.1]: npm ERR! @ start: grunt serve
      2016-03-03T17:14:35.986118+00:00 app[web.1]: npm ERR! spawn ENOENT
      2016-03-03T17:14:35.986312+00:00 app[web.1]: npm ERR! 
      2016-03-03T17:14:35.986492+00:00 app[web.1]: npm ERR! Failed at the @ start script 'grunt serve'.
      

      【讨论】:

      • 我也测试了 "postinstall": "bower install && grunt serve" 但在 laoding 上同样的问题
      • 我在几个小时后解决了整个问题。我没有使用 server.js,而是通过“start: grunt mytask”传递。我也遇到了 bower 和 node 库的问题,所以我通过更新 gitgnore 更改了所有这些。很高兴知道:在迁移到 grunt 任务之前,我尝试了 Express,但我没有发现它比基本的粗略 Node 默认脚本更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      相关资源
      最近更新 更多