Tag: javascript

  1. Assigning a variable using a switch statement in Javascript

    Here's a simple trick to assign a variable using a switch statement in Javascript. It uses IIFE pattern (http://benalman.com/news/2010/11/immediately-invoked-function-expression/). Put simply, it looks like this: var price = (function(color) { switch(color) { case 'red': return 10; case 'blue': return 20; default: return 30; } })('blue'); console.…

    on javascript node.js

  2. Setting up Express.js to Parse JSON body by default

    Sometimes you might have a client need to assume that they are always making requests with JSON body, without necessarily putting Content-Type header in their requests. //In your main index.js (or wherever you initialize express) var app = express(); //Using Express 4.x app.use(bodyParser.json({ type: '*/*'}…

    on node.js express.js javascript