我正在创建一个简单的应用程序来显示我的数据库中的联系人/用户。我根据在线发现的一些参考完成了以下内容。我收到错误'无法找到模块' ..路径/控制器/ ContactController'
app.js
var express = require('express'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose');
var app = express();
mongoose.connect("mongodb://localhost/contact");
app.use(express.bodyParser());
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/view');
app.set('view engine', 'jade');
var controllerPath = __dirname + '/controller';
require('./models/contact')(mongoose);
['Contact'].forEach(function(controller){
require(controllerPath + '/' + controller + 'Controller')(app, mongoose);
});
http.createServer(app).listen(3000, function() {
console.log('Listening on port 3000...')
})
控制器/ contact.js
var Contact = require('../models/Contact');
var ContactController = function(app,mongoose){
var Contact = mongoose.model('Contact');
app.get('/contact', function(req, req){
Contact.find({},function(err, contactinfo){
res.render("contact",{contact: contactinfo });
});
})
}
module.exports = ContactController;
模型/ contact.js
var mongoose = require('mongoose');
var ContactSchema = new mongoose.Schema({
cid: String,
name: String,
phon: Number,
contactwith: String
});
module.exports = mongoose.model('Contact', ContactSchema);
的package.json
{
"name": "SimpleAuth",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon app.js"
},
"dependencies": {
"express": "3.0.0rc4",
"jade": "*",
"mongoose": "*",
"nodemon": "*"
}
}
答案 0 :(得分:1)
您需要一条错误的路径。
require(controllerPath + '/' + controller + 'Controller')(app, mongoose);
此连接与此文件名不匹配:controller / contact.js
答案 1 :(得分:0)
更改forEach循环如下
['contact'].forEach(function(controller){
require(controllerPath + '/' + controller)(app, mongoose);
});