简体   繁体   English

页面不会使用node.js进行渲染并表达

[英]Page won't render using node.js and express

I have set up my controllers as so... 我已经设置好了控制器...

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
};

and my routes are as so... 我的路线也是如此

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);

I can render the page landmarks/new and it works fine with no problems so i know that the router is doing its job and the file set up is fine 我可以呈现页面landmarks/new并且可以正常工作,没有任何问题,所以我知道路由器正在执行其工作并且文件设置很好

However when I got to http://localhost:8000/landmarks the page hangs and i get no errors in my terminal, any advice? 但是,当我到达http://localhost:8000/landmarks ,页面挂起,并且在终端中没有出现任何错误,有什么建议吗?

I am exporting all of them and using app.use(router); 我正在导出所有这些文件并使用app.use(router); in my server.js 在我的server.js中

You have a problem with your route. 您的路线有问题。 You should do instead: .then((landmark) => res.render('/', { landmark })) for you to access it in your browser at http://localhost:8000/landmarks . 您应该改为: .then((landmark) => res.render('/', { landmark }))以便您在浏览器中的http://localhost:8000/landmarks上访问它。 What you are writing now you will access it at: http://localhost:8000/landmarks/landmarks/index . 您现在正在写的内容将在以下位置访问: http://localhost:8000/landmarks/landmarks/index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM