使用MEAN应用程序无法在heroku上使用mLab

时间:2016-09-04 11:40:30

标签: node.js mongodb heroku mean-stack mlab

我尝试使用mongoose在heroku上部署MEAN应用。该网站已部署,但无法正常运行,mLab不会使用我的模型或API路线。 我认为这可能是项目结构,但我根本不确定。

package.json是正确的,它有:" start":" node ./bin/www"

www file:

#!/usr/bin/env node
var app = require('../server');
var debug = require('debug')('AcademiaWeb:server');
var http = require('http');

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

var server = http.createServer(app);

server.listen(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;

  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);
}

server.js(它被命名为app.js,但它还没有工作,所以我按照heroku教程更改了它):

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');

require('./config/connectMongoose.js');

require('./models/asignatura_model');
require('./models/horario_model');*/
require('./models/grado_model');
require('./models/universidad_model');
require('./models/campus_model');

var asignaturas = require('./routes/api/v1/asignaturas');
var dias = require('./routes/api/v1/dias');*/
var grados = require('./routes/api/v1/grados');
var universidades = require('./routes/api/v1/universidades');
var campus = require('./routes/api/v1/campus');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

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('/api/v1/asignaturas', asignaturas);
app.use('/api/v1/grados', grados);
app.use('/api/v1/universidades', universidades);
app.use('/api/v1/campus', campus);

app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

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
        });
    });
}

app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

./ models / campus_model是mongoose模型所在的位置./routes/api/v1/asignaturas是API路径所在的位置。

connectMongoose:

'use strict';

var mongoose = require('mongoose');
mongoose.connect(process.env.MONGOLAB_URI||'mongodb://localhost/hypathia');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'Mongodb connection error:'));
db.once('open', function() {
    console.info('Connected to mongodb on port 27017');
});


module.exports = mongoose;

很抱歉这段代码,谢谢

1 个答案:

答案 0 :(得分:0)

以防有人检查这个问题,我通过从头开始提供数据库来解决这个问题