我有一个奇怪的问题,或者我错过了什么,所以请你帮忙!
问题是我在mLab上部署了我的数据库并配置了我的app.js以便它可以运行它:
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');
var api = require('./routes/api');
var routes = require('./routes/index');
var users = require('./routes/users');
var session = require('express-session');
var app = express();
app.use('/api', api);
var mongoose = require('mongoose');
mongoose.connect('mongodb://<user>:<pass>@ds019624.mlab.com:19624/heroku_b036xcp8');
var db = mongoose.connecetion;
app.use(cookieParser());
app.use(session({ secret: 'sesija', cookie: { maxAge: 60000 }, resave: true, saveUninitialized: true }));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
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
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
你可以看到有一行显示app.use('/api', api);
,这是我的路径文件夹中的API。当前的Folder结构是这样的:
- 模型
- 观看次数
- 路线
----- api.js
- app.js
api.js位于routes文件夹中。
当我执行localhost / api /某些东西时,我得到API的结果但是在heroku上我得到一个超时错误,代码或文件夹结构中有什么问题吗?
编辑: api.js
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Page= require('../models/page.js');
var adminUser= require('../models/admin-users.js');
var bodyParser = require('body-parser').json();
var bcrypt = require('bcrypt-nodejs');
/* User Routes. */
router.get('/admin', function(req, res) {
res.send('Welcome to the API zone');
});
router.get('/pages', function(request, response) {
return Page.find(function(err, pages) {
if (!err) {
return response.send(pages);
} else {
return response.send(500, err);
}
});
});
router.post('/pages/add',bodyParser, function(request, response) {
var page = new Page({
title: request.body.title,
url: request.body.url,
content: request.body.content,
menuIndex: request.body.menuIndex,
date: new Date(Date.now())
});
page.save(function(err) {
if (!err) {
return response.send(200, page);
} else {
return response.send(500,err);
}
});
});
router.post('/pages/update',bodyParser, function(request, response) {
var id = request.body._id;
var newid = mongoose.Types.ObjectId();
Page.update({
_id: id
}, {
$set: {
title: request.body.title,
url: request.body.url,
content: request.body.content,
menuIndex: request.body.menuIndex,
date: new Date(Date.now())
}
}).exec();
response.send("Page updated");
});
router.get('/pages/delete/:id', bodyParser, function(request, response) {
var id = request.params.id;
Page.remove({
_id: id
}, function(err) {
return console.log(err);
});
return response.send('Page id- ' + id + ' has been deleted');
});
// -----
router.get('/pages/admin-details/:id', bodyParser, function(request, response) {
var id = request.params.id;
Page.findOne({
_id: id
}, function(err, page) {
if (err)
return console.log(err);
return response.send(page);
});
});
router.get('/pages/details/:url', function(request, response) {
var url = request.params.url;
Page.findOne({
url: url
}, function(err, page) {
if (err)
return console.log(err);
return response.send(page);
});
});
//Admin panel login
router.post('/add-user', bodyParser, function(request, response) {
var salt, hash, password;
password = request.body.password;
salt = bcrypt.genSaltSync(10);
hash = bcrypt.hashSync(password, salt);
var AdminUser = new adminUser({
username: request.body.username,
password: hash
});
AdminUser.save(function(err) {
if (!err) {
return response.send('Admin User successfully created');
} else {
return response.send(err);
}
});
});
router.post('/login', bodyParser ,function(request, response) {
console.log("this is login api");
var username = request.body.username;
var password = request.body.password;
adminUser.findOne({
username: username
}, function(err, data) {
if (err | data === null) {
return response.send(401, "User Doesn't exist");
} else {
var usr = data;
if (username == usr.username && bcrypt.compareSync(password, usr.password)) {
// request.session.regenerate(function() {
// request.session.user = username;
return response.send(username);
//});
} else {
return response.send(401, "Bad Username or Password");
}
}
});
});
router.get('/logout', function(request, response) {
request.session.destroy(function() {
return response.send(401, 'User logged out');
});
});
function sessionCheck(request,response,next){
//check cookie
if(request.session.user) next();
else response.send(401,'authorization failed');
}
module.exports = router;
Heroku错误:
2016-06-26T18:17:07.046803 + 00:00 heroku [router]:at = error code = H12 desc =“Request timeout”method = GET path =“/ api / pages”host = app.herokuapp。 com request_id = f314665a-9378-437a-a648-c388391fd4c1 fwd =“109.163.155.179”dyno = web.1 connect = 0ms service = 30001ms status = 503 bytes = 0