我正在构建一个我正在部署到heroku的Node / Postgres应用程序。尝试在生产中打开应用程序时,我收到超时错误。根据Heroku的说法,我收到的错误来自数据库或端口连接问题。我相信我的数据库连接很好,我得到一个连接成功的日志。不确定我与postgres的集成是否导致了PORT连接问题。
这是heroku日志的错误(不是很有帮助)
2017-07-20T17:44:53.432603+00:00 heroku[web.1]: State changed from starting to crashed
2017-07-20T17:44:53.314516+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-07-20T17:44:53.314641+00:00 heroku[web.1]: Stopping process with SIGKILL
// app.js
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require("body-parser");
const logger = require('morgan');
const pg= require('pg');
const index = require('./server/routes/index');
const users = require('./server/routes/users');
const rideRequests = require('./server/routes/riderequests');
const driveRequests = require('./server/routes/driverequests');
const notifications = require('./server/routes/notifications');
const trips = require('./server/routes/trips');
const email = require('./server/routes/mail');
const app = express();
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client
.query('SELECT table_schema,table_name FROM information_schema.tables;')
.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
app.set('views', path.join(__dirname, './dist'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, './dist')));
app.use(function (req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'POST, PUT, GET, PATCH, DELETE, OPTIONS');
next();
});
app.use('/email', email);
app.use('/trip', trips);
app.use('/notifications', notifications);
app.use('/users', users);
app.use('/ride-request', rideRequests);
app.use('/drive-request', driveRequests);
app.use('/', index);
app.use('*', index);
module.exports = app;
// bin / www
#!/usr/bin/env node
var app = require('../app');
var debug = require('debug')('atlas:server');
var http = require('http');
var models = require('../server/models');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
models.sequelize.sync().then(function() {
server.listen(port, function() {
debug('Express server listening on port ' + server.address().port);
});
server.on('error', onError);
server.on('listening', onListening);
});
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
// config / config.json
{
"development": {
"username": "****",
"password": "****",
"database": "*_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "****",
"password": "****",
"database": "*_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"use_env_variable": "DATABASE_URL",
"username": "****",
"password": "****",
"database": "*_test",
"host": "*-*.herokuapp.com",
"dialect": "postgres"
}
}
上面的所有星号都包含相同的敏感信息
// models / index.js
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
谢谢!
****在部署时成功连接到postgres的其他日志。我想要注意的是,这不是在尝试打开应用程序时,我没有在应用程序打开的postgres上收到任何形式的服务器。
2017-07-20T18:36:16.953360+00:00 app[web.1]: (node:17) DeprecationWarning: PG.connect is deprecated - please see the upgrade guide at https://node-postgres.com/guides/upgrading
2017-07-20T18:36:16.991855+00:00 app[web.1]: Connected to postgres! Getting schemas...
*****更新
我认为这是一个港口问题。我在js文件中放了几个日志,所有内容都在模型/索引中顺利记录,但我现在正在登录我的www文件。我相信这就是问题所在。
答案 0 :(得分:-1)
我的问题实际上与sequelize或我的应用程序设置无关。我的package.json文件使用app.js而不是bin / wwww用于npm start。因此app.js和models / index被调用但不是bin / www中的实际服务器。有人想在heroku日志中立即崩溃解决这个问题,这是我的建议。在您的应用服务器或应用程序文件中放置一些console.log()并查看调用的内容。当呼叫停止时,这就是你的问题所在。