Tag Archives: edit a page Node.js

Building an e-Commerce website with Node – Part 26 – Edit profile page in Node JS

In this post I will show you how to add an edit profile page feature to your Node.js web application.

First, we need to create two routes, both of them with the same URL, one is GET and another one is POST. Open routes/user.js and at the end of the file, just above the module.exports = router;, let’s create the routes.


router.get('/edit-profile', function(req, res, next) {
  res.render('accounts/edit-profile', { message: req.flash('success')});
});

router.post('/edit-profile', function(req, res, next) {
  User.findOne({ _id: req.user._id }, function(err, user) {

    if (err) return next(err);

    if (req.body.name) user.profile.name = req.body.name;
    if (req.body.address) user.address = req.body.address;

    user.save(function(err) {
      if (err) return next(err);
      req.flash('success', 'Successfully Edited your profile');
      return res.redirect('/edit-profile');
    });
  });
});

Continue reading