将Sequelize API部署到Heroku的问题

时间:2018-11-04 00:12:01

标签: javascript node.js postgresql heroku sequelize.js

我正在尝试将使用Sequelize和Postgres构建的简单CRUD API部署到Heroku,但是我只是收到一些有关失败原因的神秘日志消息。

我的sequlize.js文件如下:

const UserModel = require("./user")
const HoursLoggedModel = require('./hourslogged');

const path = require('path');
const Sequelize = require('sequelize');
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];


if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

const User = UserModel(sequelize, Sequelize)
const HoursLogged = HoursLoggedModel(sequelize, Sequelize)

HoursLogged.belongsTo(User);

sequelize.sync() 
  .then(() => {
    console.log("Database & tables running")
  })


module.exports = {
  User,
  HoursLogged,
}

我要导入两个简单模型的地方。

我在其中设置了环境变量的config.js文件如下:

module.exports = {
"development": {
  "username": "root",
  "password": "password",
  "database": "vueapp",
  "host": "localhost",
  "dialect": "postgres"
},
"production": {
  "use_env_variable": process.env.DATABASE_URL,
  "dialect": "postgres"
}

}

还有index.js文件,它可以提供所有内容:

const express = require("express");
const bodyParser = require("body-parser");
const routes = require("./routes/api");


// set up express app
const app = express();

app.use(bodyParser.json());

// intialize and use routes
app.use("/api", routes)

// error handling middleware
app.use((err, req, res, next) => {
    res.status(422).send({error: err.original.detail}) // 422 makes sure status is not 200 even though error exists
})

app.listen(process.env.port || 4000, () => {
    console.log("listening for requests");
})

当我尝试使用heroku logs --tail进行调试时,我只会收到以下错误消息:

2018-11-03T23:49:53.946566+00:00 heroku[web.1]: State changed from crashed to starting
2018-11-03T23:49:55.835819+00:00 heroku[web.1]: Starting process with command `npm start`
2018-11-03T23:49:57.850511+00:00 app[web.1]: 
2018-11-03T23:49:57.850530+00:00 app[web.1]: > postgres-api@1.0.0 start /app
2018-11-03T23:49:57.850532+00:00 app[web.1]: > node index
2018-11-03T23:49:57.850533+00:00 app[web.1]: 
2018-11-03T23:49:58.341837+00:00 app[web.1]: url.js:152
2018-11-03T23:49:58.341874+00:00 app[web.1]: throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
2018-11-03T23:49:58.341875+00:00 app[web.1]: ^
2018-11-03T23:49:58.341876+00:00 app[web.1]: 
2018-11-03T23:49:58.341878+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined

url.js是什么-heroku提供了什么?

0 个答案:

没有答案