OKの一つの製品(大学)を埋めてきたものの
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const ejsmate = require('ejs-mate');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const flash = require('express-flash');
const LocalStrategy = require('passport-local').Strategy;
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
var secret = require('./config/secret');
var User = require('./models/user');
var Category = require('./models/category');
var Universities = require('./models/universities');
var app = express();
mongoose.Promise = global.Promise;
var promise = mongoose.connect(secret.database,{useMongoClient: true}, (err) =>{
if(err){
console.log(err)
}else{
console.log('connected to new database');
}
})
// MiddleWare
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: secret.secretKey,
store: new MongoStore({url: secret.database, autoReconnect: true})
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next)=>{
res.locals.user = req.user;
next();
})
app.use((req, res, next)=>{
Category.find({}, (err, categories)=>{
if(err) return next(err)
res.locals.categories = categories;
next();
})
})
app.set("view engine", "ejs");
app.engine('ejs', ejsmate);
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/public"));
var mainRoutes = require('./routes/main');
var userRoutes = require('./routes/user');
var adminRoutes = require('./routes/admin');
var apiRoutes = require('./api/api');
app.use(mainRoutes, function(req, res, next){
next();
})
app.use(userRoutes, function(req, res, next){
next();
})
app.use(adminRoutes, function(req, res, next){
next();
});
app.use('/api', apiRoutes, function(req, res, next){
next();
});
app.listen(secret.port, function(){
console.log(`app is runing on port. ${secret.port}`);
})
Main.js file
const express = require('express');
const router = express.Router();
var Product = require("../models/product");
router.get('/', function(req, res){
res.render("main/home");
})
router.get('/about', function(req, res){
res.render("main/about");
})
router.get('products/:id', function(req, res, next){
Product
.find({ category: req.params.id })
.populate('category')
.exec(function(err, products){
if(err) return next(err)
res.render("main/category", {products: products})
})
});
router.get("/product/:id", (req, res, next)=>{
Product.findById({_id: req.params.id}, (err, product)=>{
if(err) return next(err);
res.render("main/product", {
product: product
})
})
})
module.exports = router;
category.ejs
<% layout('layout') -%>
<div class = "container">
<div class ="row">
<% for (var i = 0; i < products.length; i ++){%>
<div class="col-md-4">
<a href="/product/<%=products[i]._id %>">
<div class="thumbnail">
<img src="<%=products[i].image %>">
<h3><%= products[i].name%></h3>
<p><%=products[i].category.name%></p>
<p><%=products[i].intro%></p>
<p><%=products[i].website%></p>
</div>
</a>
</div>
<%}%>
</div>
</div>
は、どのように私は、カテゴリの製品を見るようにしてくださいルーターが商品の前に "/"を追加して、router.get商品のURLに追加してください。
ありがとうございました。
インデントが適切でないコードを読みにくくなります。 – jfriend00
あなたの 'main.js'ファイルの'/'router.get( 'products /:id')'の先頭に '/'がありません –