管道变量到express中的res.render函数参数名称

时间:2016-09-12 21:31:17

标签: node.js express

我刚刚编写了一些将JSON数据加载到DOM中的代码。这是完整的代码:

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;

目前它只加载para1的JSON数据,正如您所期望的那样。我需要的是:我写的duckStorySection.para1我希望在id变量中输入,以便如果地址输入是/BoaDuck/2,那么res.render }呈现与' para2'相对应的JSON数据。 duckStorySection中的键/值对。所以它应该大致像:

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

只有id参数中管道的正确语法。这是怎么做到的?

2 个答案:

答案 0 :(得分:1)

您可以采取额外步骤,因此您可以拨打var id = req.params.id;来解决问题,而不是让var id = 'Para'+req.params.id;代替res.render('Boa-Duck/BoaDuckA', { storyText: duckStorySection[id]});。虽然假设您依赖整数索引,但这种解决方案并不合适。我建议直接使用整数ID索引对象

答案 1 :(得分:0)

鉴于您的Web服务API依赖于数字索引(/BoaDuck/:id其中:id是非负整数),使用故事对象中的键不适合您的问题。只需为每个条目提供您希望它们检索的确切密钥:

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: ""
};

或者,使用数组:

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 ...",
  ""
];

然后直接在字典上使用id键:

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