我想在Node.js上显示模型值,但是浏览器返回以下错误。我想获取产品数据并显示它,但似乎index.js不会将产品数据传递给index.ect。你能说出如何解决这个问题吗?
错误:无法查找视图"错误"在views目录" / Users / toshihirokaseda / bitcore_test / Realarts / views"
//collection.js
var express = require('express');
var router = express.Router();
var model = require('../model.js'),
User = model.User;
/* GET users listing. */
router.get('/collection', function(req, res, next) {
res.render('collection', { name: req.session.user});
});
module.exports = router;
//views/collection.ect
<!doctype html>
<% extend 'layout' %>
<div class="col-xs-12 col-sm-10 col-md-8 col-lg-8 col-sm-offset-1 col-md-offset-2 col-lg-offset-2">
<div class="status_bar">
<div class="form-group has-feedback has-feed-back-right">
<div class="row">
<div class="col-xs-4 col-xs-offset-8">
<input type="text" class="form-control" id="search" placeholder="search">
</div>
</div>
</div>
</div>
<% include 'partials/product_card.ect' %>
</div
//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 session = require('express-session');
var routes = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var register_product = require('./routes/register_product');
var collection = require('./routes/collection');
var ECT = require('ect');
var app = express();
var MongoStore = require('connect-mongo')(session);
app.engine('ect', ECT({ watch: true, root: __dirname + '/views', ext: '.ect' }).render);
app.set('view engine', 'ect');
app.use(express.static(__dirname + '/public'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: new MongoStore({
db: 'session',
host: 'localhost',
clear_interval: 60 * 60
}),
cookie: {
httpOnly: false,
maxAge: 30 * 60 * 1000 // 30min.
}
}));
var sessionCheck = function(req, res, next) {
if (req.session.user) {
next();
} else {
res.redirect('/login');
}
};
//routing
app.use('/login', login);
app.get('/users', users);
app.get('/collection', collection);
app.get('/register_product', register_product);
app.use('/', routes);
app.use('/edit', routes);
app.use('/delete', routes);
//index.js
var express = require('express');
var router = express.Router();
var model = require('../model/product.js');
var Product = model.Product;
router.get('/', function(req, res, next){
Product.find({}, function(err, items){
res.render('index', { title: 'Menu', items:items})
});
});
//index.ect
<!doctype html>
<ul>
<% items.forEach(function(item){ %>
<li><%= item.name %>, <%= item.price %> <li>
<% }) %>
</ul>
//model/product.js
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/session');
function validator(v) {
return v.length > 0;
}
var Product = new mongoose.Schema({
name : {
type: String, validate: [validator, "Empty Error"]
},
price : {
type: Number,
default: 500
},
order_user : {
type: Array,
default: []
},
created: {
type: Date,
default: Date.now
}
});
exports.Product = db.model('Product', Product);
答案 0 :(得分:2)
试试这个:
var ectRenderer = ECT({ watch: true, root: __dirname + '/views', ext : '.ect' });
app.set('views', path.join(__dirname,'/views'));
app.engine('ect',ectRenderer.render);
app.set('view engine','ect');
你应该有目录结构:
Realarts/
├──_views
├──index.ect
同样在index.js
文件中,您没有导出router
。您需要在collection.js
:
module.exports = router;