Javascript 页面赢得';t使用node.js和express进行渲染

Javascript 页面赢得';t使用node.js和express进行渲染,javascript,node.js,express,Javascript,Node.js,Express,我已经设置了控制器,以便 const Landmark = require('../models/landmark'); function indexRoute(req, res, next) { Landmark .find() .exec() .then((landmark) => res.render('/landmarks/index', { landmark })) .catch(next); } function newRoute(req, res) {

我已经设置了控制器,以便

const Landmark = require('../models/landmark');
function indexRoute(req, res, next) {
  Landmark
  .find()
  .exec()
  .then((landmark) => res.render('/landmarks/index', { landmark }))
  .catch(next);
}

function newRoute(req, res) {
  return res.render('landmarks/new');
}

function createRoute(req, res, next) {
  req.body.createdBy = req.user;
  Landmark
  .create(req.body)
  .then(() => res.redirect('/landmarks'))
  .catch((err) => {
    if(err.name === 'ValidationError') return res.badRequest(`/landmarks/${req.params.id}/edit`, err.toString());
    next(err);
  });
}

function showRoute(req, res, next) {
  Landmark
  .findById(req.params.id)
  .populate('createdBy comments.createdBy')
  .exec()
  .then((landmark) => {
    if(!landmark) return res.notFound();
    return res.render('landmarks/show', { landmark });
  })
  .catch(next);
}

function editRoute(req, res, next) {
  Landmark
  .findById(req.params.id)
  .exec()
  .then((landmark) => {
    if(!landmark) return res.redirect();
    if(!landmark.belongsTo(req.user)) return res.unauthorized(`/landmarks/${landmark.id}`, 'You do not have permission to edit that resource');
    return res.render('landmarks/edit', { landmark });
  })
  .catch(next);
}
function updateRoute(req, res, next) {
  Landmark
  .findById(req.params.id)
  .exec()
  .then((landmark) => {
    if(!landmark) return res.notFound();
    if(!landmark.belongsTo(req.user)) return res.unauthorized(`/landmarks/${landmark.id}`, 'You do not have permission to edit that resource');
    for(const field in req.body) {
      landmark[field] = req.body[field];
    }
    return landmark.save();
  })
  .then(() => res.redirect(`/landmarks/${req.params.id}`))
  .catch((err) => {
    if(err.name === 'ValidationError') return res.badRequest(`/landmarks/${req.params.id}/edit`, err.toString());
    next(err);
  });
}
function deleteRoute(req, res, next) {
  Landmark
  .findById(req.params.id)
  .exec()
  .then((landmark) => {
    if(!landmark) return res.notFound();
    if(!landmark.belongsTo(req.user)) return res.unauthorized(`/landmarks/${landmark.id}`, 'You do not have permission to delete that resource');
    return landmark.remove();
  })
  .then(() => res.redirect('/landmarks'))
  .catch(next);
}
function createCommentRoute(req, res, next) {
  req.body.createdBy = req.user;
  Landmark
  .findById(req.params.id)
  .exec()
  .then((landmark) => {
    if(!landmark) return res.notFound();
    landmark.comments.push(req.body); // create an embedded record
    return landmark.save();
  })
  .then((landmark) => res.redirect(`/landmarks/${landmark.id}`))
  .catch(next);
}
function deleteCommentRoute(req, res, next) {
  Landmark
  .findById(req.params.id)
  .exec()
  .then((landmark) => {
    if(!landmark) return res.notFound();
    // get the embedded record by it's id
    const comment = landmark.comments.id(req.params.commentId);
    comment.remove();
    return landmark.save();
  })
  .then((landmark) => res.redirect(`/landmarks/${landmark.id}`))
  .catch(next);
}
module.exports = {
  index: indexRoute,
  new: newRoute,
  create: createRoute,
  show: showRoute,
  edit: editRoute,
  update: updateRoute,
  delete: deleteRoute,
  createComment: createCommentRoute,
  deleteComment: deleteCommentRoute
};
我的路线也是如此

const express = require('express');
const router  = express.Router();
// const sessions = require('../controllers/sessions');
// const registrations = require('../controllers/registrations');
const landmark = require('../controllers/landmarks');
router.get('/', (req, res) => res.render('statics/index'));
router.route('/landmarks')
.get(landmark.index)
.post(landmark.create);
router.route('/landmarks/new')
.get(landmark.new);
router.route('/landmarks/:id')
.get(landmark.show)
.put(landmark.update)
.delete(landmark.delete);
router.route('/landmarks/:id/edit')
.get(landmark.edit);
module.exports(router);
我可以渲染页面
landmarks/new
,它工作正常,没有问题,因此我知道路由器正在完成它的工作,文件设置也很好

但是当我进入
http://localhost:8000/landmarks
页面挂起,我的终端没有错误,有什么建议吗


我正在导出它们并使用
app.use(路由器)
在my server.js中,您的路线有问题。您应该这样做:
。然后((landmark)=>res.render(“/”,{landmark}))
以便在
http://localhost:8000/landmarks
。您现在正在编写的内容将在以下位置访问:
http://localhost:8000/landmarks/landmarks/index