我正在尝试使用mongoose在heroku上部署一个MEAN应用程序。网站已经部署,但是不能正常工作,mLab没有使用我的模型和API路由。我想可能是项目结构的问题,但我也不确定。
包。Json是正确的,它有:"start": "node ./bin/www"
www文件:
#!/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是猫鼬模型所在的目录,./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;
很抱歉写了这么多代码,谢谢
为了防止有人检查这个问题,我通过从头开始提供数据库来解决这个问题