Git Product home page Git Product logo

Comments (10)

dougwilson avatar dougwilson commented on June 10, 2024

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.

jonathanong avatar jonathanong commented on June 10, 2024

yeah i dunno why you would ever want to do that... do a switch

from accepts.

dougwilson avatar dougwilson commented on June 10, 2024

...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.

binarykitchen avatar binarykitchen commented on June 10, 2024

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.

jonathanong avatar jonathanong commented on June 10, 2024

that's not how content negotiation works though

from accepts.

dougwilson avatar dougwilson commented on June 10, 2024

@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.

binarykitchen avatar binarykitchen commented on June 10, 2024

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.

dougwilson avatar dougwilson commented on June 10, 2024
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.

binarykitchen avatar binarykitchen commented on June 10, 2024

Thanks so much @dougwilson - after all your help I owe you one beer!

from accepts.

dougwilson avatar dougwilson commented on June 10, 2024

🍺

from accepts.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.