1

I am trying to send data from one view to another but i am able to redirect only but my requirement is to redirect and send data at once to another jade template.I am using below code for this:

customer.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res) {
    res.render('customers', { title: 'Express' });
});
router.post('/customers/add', function (req, res) {
    var name = req.param('name');// req.body.name;//second way
    var address = req.param('address');
    var email = req.param('email');
    var phone = req.param('phone');
    res.redirect('/ViewCutomerDetail');
})
module.exports = router;

Now what i want is to send name,address,email,phone to another view say "ViewCutomerDetail" but how can i do this on "ViewCutomerDetail" i am using below code

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res) {
    console.log('hello');
    res.render('ViewCutomerDetail', { req: req});
});

module.exports = router;
posit labs
  • 8,951
  • 4
  • 36
  • 66
stylishCoder
  • 385
  • 1
  • 19

1 Answers1

1

You can use cookies.

http://expressjs.com/en/api.html#req.cookies

router.post('/customers/add', function (req, res) {
    res.cookie('name', req.param('name'));
    res.cookie('address', req.param('address'));
    res.cookie('email', req.param('email'));
    res.cookie('phone', req.param('phone'));
    res.redirect('/ViewCutomerDetail');
})

/* GET users listing. */
router.get('/', function (req, res) {
    console.log('hello');
    res.render('ViewCutomerDetail', { req: req, cookies: req.cookies });
});
posit labs
  • 8,951
  • 4
  • 36
  • 66
  • i am not sure about it is the right way..can't we send like in mvc we send object with redirect action here..because right now we are talking about only 4 fields ..what about list ??????? – stylishCoder Sep 28 '16 at 04:13
  • Sorry, I had the wrong syntax for setting the cookies. I've updated the answer. – posit labs Sep 28 '16 at 17:00
  • @stylishCoder "the right way" would be using sessions, but you don't want to (or can't) use them. You need _some_ sort of storage that persists between requests. – robertklep Sep 28 '16 at 17:40