Express.js 路线挂在除主页以外的所有地方

Express.js Routes Hang on everything but the homepage

我正在构建一个应用程序并学习 MEAN 堆栈。我昨晚成功 followed a tutorial on thinkster 并且能够让一切按预期工作。然而,现在,当我自己尝试这样做时,我 运行 遇到了问题。我的应用 "burbank" 的主页加载正常,但我创建的 3 条路线只是挂起。没有错误,终端中没有任何内容,只是不断尝试加载。

localhost:3000 加载正常

localhost:3000/contacts 挂起

index.js

var mongoose = require('mongoose');
var Contact = mongoose.model('Contact');
var Event = mongoose.model('Event');
var Vehicle = mongoose.model('Vehicle');

var express = require('express');
var router = express.Router();

router.get('/contacts', function(req, res, next) {
  Contact.find(function(err, contacts){
    if(err){ return next(err); }

    res.json(contacts);
  });
});

router.get('/events', function(req, res, next) {
  Event.find(function(err, events){
    if(err){ return next(err); }

    res.json(events);
  });
});

router.get('/vehicles', function(req, res, next) {
  Vehicle.find(function(err, vehicles){
    if(err){ return next(err); }

    res.json(vehicles);
  });
});

App.js

var mongoose = require('mongoose');
mongoose.connect = ('mongodb://localhost/burbank');
require('./models/Contacts');
require('./models/Events');
require('./models/Vehicles');

var routes = require('./routes/index');
var users = require('./routes/users');

我最初认为这与我在 app.js 中放置需求和变量的顺序有关,但我认为情况并非如此。无论如何,非常感谢您的帮助。我正在慢慢地 掌握所有这些概念。

尝试检查路由是否在没有猫鼬层的情况下使用:res.send("example");

在 mongoose 中使用 .find 函数时,我认为您必须传递一个空对象作为第一个参数(如果您想 return 整个集合)。

尝试改变这个:

Contact.find(function(err, contacts){

为此:

Contact.find({}, function(err, contacts){

对所有查找查询重复该操作。

根据 Mongoose 文档,find 的第一个参数是包含要应用于文档的过滤器的字典:

http://mongoosejs.com/docs/api.html#model_Model.find

尝试这样的事情:

Contact.find().exec(function(err, contacts) { ...

有时只需要离开一个周末,在大峡谷徒步旅行,在车里坐 16 个小时,就能找到语法错​​误。

connect.mongoose = ('mongodb://localhost/burbank');

不等于

connect.mongoose('mongodb://localhost/burbank');


感谢那些帮助过我的人,非常感谢。原谅我羞愧地低下了头