Heroku需要一个server.js文件。 放什么?

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

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

但是,我可以在server.js中放置什么? 我发现了很多例子,但对我来说没有任何明确的或相关的。 我对这个需求有点困惑。

该应用程序的设置是相当普遍:npm安装/ bower安装,然后“grunt服务”启动应用程序。 该应用程序是一个简单的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' ]); }); }; 

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

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

这对我来说似乎更符合逻辑:)所以我testing了它,部署很好,但是应用程序崩溃,出现这个错误:

 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'.