我搜索了很多stackoverflow q / a,但似乎无法解决这个问题。我正在尝试将我的节点js应用程序部署到Heroku,并不断收到错误。我可以在我当地的地方运行它。
错误:
2014-10-17T13:47:11.174606+00:00 heroku[web.1]: State changed from crashed to starting
2014-10-17T13:47:13.312806+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-10-17T13:47:14.494495+00:00 app[web.1]: module.js:340
2014-10-17T13:47:14.493848+00:00 app[web.1]:
2014-10-17T13:47:14.504444+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:338:15)
2014-10-17T13:47:14.504440+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2014-10-17T13:47:14.494889+00:00 app[web.1]: throw err;
2014-10-17T13:47:14.495090+00:00 app[web.1]: ^
2014-10-17T13:47:14.504450+00:00 app[web.1]: at node.js:906:3
2014-10-17T13:47:14.504449+00:00 app[web.1]: at startup (node.js:119:16)
2014-10-17T13:47:14.504447+00:00 app[web.1]: at Function.Module.runMain (module.js:497:10)
2014-10-17T13:47:14.504446+00:00 app[web.1]: at Function.Module._load (module.js:280:25)
2014-10-17T13:47:15.551015+00:00 heroku[web.1]: State changed from crashed to starting
2014-10-17T13:47:15.550391+00:00 heroku[web.1]: State changed from starting to crashed
2014-10-17T13:47:15.538661+00:00 heroku[web.1]: Process exited with status 8
2014-10-17T13:47:18.391309+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-10-17T13:47:19.331140+00:00 app[web.1]: ^
2014-10-17T13:47:19.333015+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2014-10-17T13:47:19.333019+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:338:15)
2014-10-17T13:47:19.330847+00:00 app[web.1]: module.js:340
2014-10-17T13:47:19.330979+00:00 app[web.1]: throw err;
2014-10-17T13:47:19.333020+00:00 app[web.1]: at Function.Module._load (module.js:280:25)
2014-10-17T13:47:19.333021+00:00 app[web.1]: at Function.Module.runMain (module.js:497:10)
2014-10-17T13:47:19.333023+00:00 app[web.1]: at startup (node.js:119:16)
2014-10-17T13:47:19.333024+00:00 app[web.1]: at node.js:906:3
2014-10-17T13:47:19.330240+00:00 app[web.1]:
2014-10-17T13:47:20.165015+00:00 heroku[web.1]: Process exited with status 8
2014-10-17T13:47:20.182699+00:00 heroku[web.1]: State changed from starting to crashe
我的app.js文件:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.get('/', function(req, res) {
res.render('index', { title: 'The index page!' })
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
my route / index.js文件:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Index' });
});
module.exports = router;
任何帮助非常赞赏
答案 0 :(得分:7)
默认情况下,Heroku会查找index.js以启动您的应用。您正在使用app.js(这是您正在使用的Express生成器提供的默认名称)。
您可以更改名称,也可以使用Procfile指定自定义名称。
要创建Procfile,只需在主目录中创建一个名为Procfile(无扩展名)的新文件。它应该包含这一行:
web: node app.js