James Wellington James Wellington - 1 year ago 63
Node.js Question

piping variables into res.render function parameter names in express

I have just written some code which loads JSON data into the DOM. This is the full code:

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

// var money = require('./routes/money');

var duckStorySection = {
para1: "I first met Boa-Duck through Lene-Cow's intervention. I had been scoping out the possibility of an illicit affair with The Green Party of England And Wales' Amelia Womack, but the party propaganda machine had other ideas. Indeed, as soon as I began to show interest the militant ecological wing and more threatening sections of The Womens' Society and LGBTQI (I have a suspicion many election posts are only contested by a single candidate because of internal factional bullying and whispering campaigns, mainly originating from these factions, but I have no direct evidence of causation in these cases) began a concerted campaign of what is, in the parlance of certain political operatives, known as 'this.'",
para2: "But this is not a story about The Green Party of England and Wales, it is a story of a group of forest animals and their one human companion: myself (Dragon-Bear), Boa-Duck, Lene-Cow and The Master Priest. As I may have alluded to already, I have recently begun an affair with Boa-Duck, and it is with Lene-Cow, my fiancee's, mildly mixed-blessing. She is an anarchist revolutionary and due to her youth and certain affectations about her countenance I had thought her to be naive. She is not. She is slowly developing a network of squatters and upper-middle class drop-outs which she will be using as informants. She also kisses incredibly well, the silk-like seams between her lips opening just a fraction to allow me the merest suggestion of her tongue (I've plagiarised that line from somewhere, but my concern with this piece is far more to inform than to entertain, so you'll have to bear with me).",
para3: "Boa-Duck is a fairly small duck, by duck standards. She has subtle, delicate fur that is particularly pleasant to run your hands over and around, though I was careful not to brush too fast or with too much urgency as I am an older dragon-bear and I am stridently aware that the possibility of breaking a much younger duck with overly strong affection is ever-present. This, of course, risks overplaying my hand and making me out to be much more powerful than I am, so for balance I must mention several things about myself. The first is that I am poor, excessively so, that I work for a living creating websites which make no money for me whatsoever, as yet, and exist only on the charity of those who understand my situation. The second, which is a particularly galling consideration considering the first issue, and the occasional flareups of dragon temper and temperament which I am occasionally prone to, is that I have for some considerable time been the victim of a concerted campaign of character assassination which began when I started working on my first, never completed, website: Lake of Birds, but which seems to stem from times further past, from my work in technical support and my ever dwindling length of employment, spurred on by my trade union activism, anarchist disruption campaigns, alcoholism, mental health issues or my increasing inability to keep and hold friends, depending on who you believe (I maintain the first two, as a matter of course and, for my perspective at least, certain and unerring truth).",
para4: "Oh yes, and when I searched her bag I found an envelope addressed to the local DCI, in which no letter had yet been placed.",
para5: ""

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('stories', { title: 'Order Of The Mouse: Phase 1.7 -- Operation Silk Scarf (Narratives)' });

router.get('/BoaDuck/:id', function(req, res, next) {
var id = req.params.id;
res.render('Boa-Duck/BoaDuckA', { storyText: duckStorySection.para1 });

module.exports = router;

At the moment it only loads the JSON data for para1, as you'd expect. What I need is: where I have written
I would like to pipe in the
variable, so that if the address input is, for example,
, the
renders the JSON data corresponding with the 'para2' key/value pair within
. So it should do something roughly like:

res.render('Boa-Duck/BoaDuckA', { storyText: duckStorySection.para[id] })

Only with whatever the correct syntax is for piping in the
parameter. How is this done?

Answer Source

Given that your web service API relies on numerical indices (/BoaDuck/:id where :id is a non-negative integer), using keys like those in your story object is inappropriate for your problem. Just give each entry the exact key that you want them to be retrieved by:

var duckStorySection = {
  0: "I first met Boa-Duck ...",
  1: "But this is not a story about...",
  2: "Boa-Duck is a fairly small duck, ...",
  3: "Oh yes, and when I searched her ...",
  4: ""

Alternatively, use an array:

var duckStorySection = [
  "I first met Boa-Duck ...",
  "But this is not a story about...",
  "Boa-Duck is a fairly small duck, ...",
  "Oh yes, and when I searched her ...",

Then use the id key directly over the dictionary:

router.get('/BoaDuck/:id', function(req, res, next) {
  var id = req.params.id;
  res.render('Boa-Duck/BoaDuckA', {
    storyText: duckStorySection[id]