Comments (10)
You probably want this:
switch (req.accepts('html', 'json')) {
case 'json':
// process the GET request from superagent (client)
// ..
res.json(200, {});
break;
default:
// middleware further down will load the basic page layout
next();
break;
}
from accepts.
yeah i dunno why you would ever want to do that... do a switch
from accepts.
...and the bonus of using the switch method above means you are interpreting the Accept
header correctly, unlike the original if statement (the if statement will execute the else statement for Accept: application/json, */*;q=0.1
, for example, which is probably not what you wanted to happen).
from accepts.
Hmmm... @dougwilson I do not know. I am not a fan of switch blocks because they are not OO friendly and sometimes complicate things.
How about something like req.acceptsOnly('json')
? That would be neat!
from accepts.
that's not how content negotiation works though
from accepts.
@binarykitchen a req.acceptsOnly('json')
would still return false for Accept: application/json, */*;q=0.1
, which is unlikely what you want. Also, there is nothing "not OO friendly" about switch statements. They are basically fancier if statements. You can always write it as an if if you like...
if (req.accepts('html', 'json') === 'json') {
// process the GET request from superagent (client)
// ..
res.json(200, {});
} else {
// middleware further down will load the basic page layout
next();
}
but that if pattern will not scale very well if you want to add more.
from accepts.
Ah, I see now ... thanks so much!
Can I somehow plug in a new method to the request instance through the whole express app which looks like this:
req.wantsJson = function() {
return this.accepts('html', 'json') === 'json')
}
Where could I do that? I am keen to avoid code duplication in all my controllers hence the need for such a common function.
from accepts.
function wantsJson() {
return this.accepts('html', 'json') === 'json';
}
app.use(function (req, res, next) {
req.wantsJson = wantsJson;
next();
});
is the best way to do it. Note that having the wantsJson
outside the middleware is how it will perform well since it doesn't create a new function for every request.
from accepts.
Thanks so much @dougwilson - after all your help I owe you one beer!
from accepts.
🍺
from accepts.
Related Issues (17)
- Type suffixes HOT 5
- Document `req` Requirements HOT 7
- Implementing "406: not acceptable" HOT 4
- Support charset in accept header HOT 12
- */* does not match text/* HOT 5
- Ability to add additional types
- change * default HOT 3
- possibility to get region from accept language HOT 4
- Add support for type parameters
- Cannot find module negotiator HOT 3
- Can we make this library more abstract?
- Accept types always validating HOT 2
- docs HOT 3
- Change to mime-types causes crash HOT 2
- RangeError: Maximum call stack size exceeded HOT 4
- Doc typo `req` => `res` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from accepts.