node.js - piping variables into res.render function parameter names in express -
i have written code loads json data dom. 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. had been scoping out possibility of illicit affair green party of england , wales' amelia womack, party propaganda machine had other ideas. indeed, began show interest militant ecological wing , more threatening sections of womens' society , lgbtqi (i have suspicion many election posts contested single candidate because of internal factional bullying , whispering campaigns, originating these factions, have no direct evidence of causation in these cases) began concerted campaign of is, in parlance of political operatives, known 'this.'", para2: "but not story green party of england , wales, story of group of forest animals , 1 human companion: myself (dragon-bear), boa-duck, lene-cow , master priest. may have alluded already, have begun affair boa-duck, , lene-cow, fiancee's, mildly mixed-blessing. anarchist revolutionary , due youth , affectations countenance had thought naive. not. developing network of squatters , upper-middle class drop-outs using informants. kisses incredibly well, silk-like seams between lips opening fraction allow me merest suggestion of tongue (i've plagiarised line somewhere, concern piece far more inform entertain, you'll have bear me).", para3: "boa-duck small duck, duck standards. has subtle, delicate fur particularly pleasant run hands on , around, though careful not brush fast or urgency older dragon-bear , stridently aware possibility of breaking younger duck overly strong affection ever-present. this, of course, risks overplaying hand , making me out more powerful am, balance must mention several things myself. first poor, excessively so, work living creating websites make no money me whatsoever, yet, , exist on charity of understand situation. second, particularly galling consideration considering first issue, , occasional flareups of dragon temper , temperament prone to, have considerable time been victim of concerted campaign of character assassination began when started working on first, never completed, website: lake of birds, seems stem times further past, work in technical support , ever dwindling length of employment, spurred on trade union activism, anarchist disruption campaigns, alcoholism, mental health issues or increasing inability keep , hold friends, depending on believe (i maintain first two, matter of course and, perspective @ least, , unerring truth).", para4: "oh yes, , when searched bag found envelope addressed local dci, in no letter had yet been placed.", para5: "" }; /* home page. */ router.get('/', function(req, res, next) { res.render('stories', { title: 'order of 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 moment loads json data para1, you'd expect. need is: have written duckstorysection.para1
pipe in id
variable, if address input is, example, /boaduck/2
, res.render
renders json data corresponding 'para2' key/value pair within duckstorysection
. should like:
res.render('boa-duck/boaducka', { storytext: duckstorysection.para[id] })
only whatever correct syntax piping in id
parameter. how done?
given web service api relies on numerical indices (/boaduck/:id
:id non-negative integer), using keys in story object inappropriate problem. give each entry exact key want them retrieved by:
var duckstorysection = { 0: "i first met boa-duck ...", 1: "but not story about...", 2: "boa-duck small duck, ...", 3: "oh yes, , when searched ...", 4: "" };
alternatively, use array:
var duckstorysection = [ "i first met boa-duck ...", "but not story about...", "boa-duck small duck, ...", "oh yes, , when searched ...", "" ];
then use id
key directly on dictionary:
router.get('/boaduck/:id', function(req, res, next) { var id = req.params.id; res.render('boa-duck/boaducka', { storytext: duckstorysection[id] }); });
Comments
Post a Comment