Quick Express question

Quick Express question
0

#1
var express = require('express')
var app = express()

or

var express = require('express'), app = express()

But why not

var app = require('express')()

Another question, why are there no semicolons in the below example from express:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

#2

Do you mean var app = require('express')()?
I’ve been wondering on the same thing. But there are times when you want to use express.static() along with app too, so in this case I think it’s better to use an express variable.

Maybe it’s a matter of preference.

I think it follows the standard style.


#3

Ok that makes since if you are importing other parts in express.


#4

@kevcomedia just saw this, its ES6:

Semicolons are actually optional, because ECMAScript (the standard for Node.js and browser JavaScript implementations) has an automatic semicolon-insertion feature (ASI). Here’s the draft of the ECMAScript 6 (ES6) documentation about ASI.
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-automatic-semicolon-insertion


#5

I’ve never tried (intentionally) omitting semicolons in JS (I’m comfortable with semicolons at the end of each line, thanks Java and C#). Maybe I’ll try it for once :thinking:


#6

That’s the way I feel.

You don’t have to have semicolons?


#7

there are some rare cases where omitting semi colons would potentially bring bugs

For instance:

function foo1()
{
  return {
      bar: "hello"
  };
}

vs.

function foo2()
{
  return
  {
      bar: "hello"
  };
}

Though both looks about the same, the first one will output {bar: Hello'},and the second would output undefined. JS will autmatically put semicolon after the return keyword in the second function, thus executing and exiting without going to {bar: 'hello}

Long story short, you should put semicolons :smile: