Passing Context objects through express does not work
See original GitHub issueI’ve been having an extended discussion on StackOverflow (link) with @sethkinast regarding passing Context objects through Express.
I have put together a very quick example of how to replicate this issue here: https://github.com/adamkdean/dust-test
But to save time, the script is this:
const path = require('path')
const hoffman = require('hoffman')
const express = require('express')
const request = require('request')
const duster = require('duster')
const dust = duster.dust
const app = express()
const base = dust.context({
isBase: true,
age: 26,
people: [
{ name: 'exampleA' },
{ name: 'exampleB' }
]
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'dust')
app.engine('dust', hoffman.__express())
app.get('/', function (req, res) {
res.render('people', {})
})
app.get('/pojo', function (req, res) {
res.render('people', {
age: 26,
people: [
{ name: 'exampleA' },
{ name: 'exampleB' }
]
})
})
app.get('/context', function (req, res) {
res.render('people', base.push({
people: [
{ name: 'adam' }
]
}))
})
app.listen(4000, function () {
console.log('Visit http://localhost:4000 to see streaming!')
})
If you go to /pojo
then the plain old JavaScript object renders fine (the template is {#people}<li>{name} aged {age}</li>{/people}
), but if you go to /context
then it doesn’t render fine at all.
I think this would probably be a better place to discuss the issue.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
node.js - Passing context implicitly across functions and ...
If i pass the context as a variable i have to refactor a lof of my code base and a lot of business...
Read more >apollo-server-testing context is not receiving the `req` object
The test relies on the req object being passed to the context function in the server setup, but I'm only getting an empty...
Read more >Context-aware logging in Node.js - LogRocket Blog
The issue could be fixed by passing the “context” argument to the createUser function, but that wouldn't solve anything. What if there was ......
Read more >Koa - next generation web framework for node.js
A Koa Context encapsulates node's request and response objects into a single object which provides many helpful methods for writing web applications and...
Read more >AWS Lambda context object in Node.js
AWS Lambda context object in Node.js. When Lambda runs your function, it passes a context object to the handler. This object provides methods...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s the issue: https://github.com/expressjs/express/blob/master/lib/application.js#L535
A new object,
renderOptions
, gets created. This object of course doesn’t have Context as its prototype so Dust tries to shove it into a context.It’s very interesting that no one has ever run into this before. I would never have expected Express to mangle a context object passed to it. We can fix this by dropping the instanceof check on our end.
This is available as 2.7.4.