Saturday, 10 June 2017

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

However when I got to http://localhost:8000/landmarks the page hangs and i get no errors in my terminal, any advice?

I am exporting all of them and using app.use(router); in my server.js



via James Clarke

No comments:

Post a Comment