关于express.Router()问题,萌新求教
express.Router()问题
// 定义了一个detail.js路由文件:
const express = require('express');
const router = express.Router();
const Movie = require('../model/movie');
// 获取param
router.param('id', (req, res, next, id) => {
next()
})
router.get('/movie/:id', function(req, res) {
let id = req.params.id;
Movie.findById(id, function(err, movie) {
if(err) {
console.log(err);
}
res.render('detail', {
title: 'website ' + movie.title,
movie: movie
})
});
})
module.exports = router
app.js :
const express = require('express')
const app = express()
const detail = require('./routes/detail.js')
// 失败,报错404
app.use('/movie/:id', detail)
//成功
app.use(detail)
//成功
app.get('/movie/:id', detail)