页面将无法使用node.js和express进行渲染

时间:2017-06-10 07:21:26

标签: 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) {
  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时,页面挂起,我的终端没有错误,有什么建议吗?

我正在导出所有这些并在我的server.js中使用app.use(router);

1 个答案:

答案 0 :(得分:0)

您的路线有问题。您应该改为:.then((landmark) => res.render('/', { landmark })),以便您在浏览器中http://localhost:8000/landmarks访问它。您现在正在撰写的内容是:http://localhost:8000/landmarks/landmarks/index