无法在带有 MEAN 应用程序的 heroku 上使用 mLab
Can't use mLab on heroku with MEAN app
我正在尝试使用猫鼬在 heroku 上部署 MEAN 应用程序。该网站已部署但无法正常工作,mLab 不采用我的模型或 API 路由。
我认为这可能是项目结构,但我一点也不确定。
package.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;
抱歉代码量太大,谢谢
为了防止有人检查这个问题,我通过从头开始输入数据库解决了这个问题
我正在尝试使用猫鼬在 heroku 上部署 MEAN 应用程序。该网站已部署但无法正常工作,mLab 不采用我的模型或 API 路由。 我认为这可能是项目结构,但我一点也不确定。
package.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;
抱歉代码量太大,谢谢
为了防止有人检查这个问题,我通过从头开始输入数据库解决了这个问题