在Heroku上部署Node js + mongo时出错

时间:2019-05-28 03:23:18

标签: node.js heroku

在Heroku中连接到我的第一个Nodejs应用程序时遇到问题。在本地运行没有问题,在执行heroku本地网络时也没有问题。但是当我拖尾日志时,它说:

  

2019-05-28T02:32:49.911060 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”,方法= GET path =“ / favicon.ico” host = myveryfirst.herokuapp。 com request_id = 1df1314a-bf72-4201-8b20-fb812088e633 fwd =“ 219.92.3.81” dyno = connect = service = status = 503字节= protocol = https

我已经设置了Procfile:

> web: node ./src/server.js

这是我的package.json:

{
  "name": "myveryfirst",
  "version": "1.0.0",
  "description": "",
  "main": "./src/server.js",
  "engines": {
    "node": "10.15.0",
    "npm": "6.4.1"
  },
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "jest --watch --runInBand",
    "start": "node ./src/server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lola/myveryfirst.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/lola/myveryfirst/issues"
  },
  "homepage": "https://github.com/lola/myveryfirst#readme",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cls-hooked": "^4.2.2",
    "dotenv": "^8.0.0",
    "express": "^4.17.1",
    "express-session": "^1.16.1",
    "joi": "^14.3.1",
    "js-sha512": "^0.8.0",
    "mongoose": "^5.5.11",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "path": "^0.12.7",
    "pug": "^2.0.3",
    "uuid": "^3.3.2",
    "winston": "^3.2.1"
  }
}

这是我的server.js

const express = require('express');
const cls = require('cls-hooked');
const db = require('./config/mongodb');
const passport = require('passport');
const session = require('express-session');
const config = require('./config/config');
const router = require('./routes/router');
const clsBinder = require('./middleware/clsBinder');
const logFormatter = require('./middleware/logFormatter');
const logger = require('./services/loggerService');
const path = require('path');
const bodyParser = require('body-parser');

// Initialize the express application
const app = express();

// Create a cls namespace for the correlation ID
const ns = cls.createNamespace(config.cls.correlationIdNamespace);

app.use(clsBinder(ns));
app.use(logFormatter);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Assign passport to express
require('./config/passport')(passport);

app.use(session({
  // cookie: { maxAge: 60000 },
  secret: config.sessionSettings.secret,
  resave: false,
  saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());

// Load View Engine
app.use(express.static('./'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));

// Assign the routes to express
app.use(router);

app.use((err, req, res) => {
  logger.error(err);
  res.send({ error: err.message });
});

// Connecting to DB
db();

// Start the server
app.server = app.listen(process.env.PORT || 3000, (err) => {
    if (err) {
        logger.error(err);
    }
  logger.info(`Server started successfully`);
});

module.exports = app;

0 个答案:

没有答案