Search code examples
node.jssocket.ioframeworksparentheses

What is the second pair of parenthesis in "require('express')()" in node.js?


I'm following the socket.io chat application tutorial here - https://socket.io/get-started/chat/

and it's using express framework. There's a code in the tutorial that is:

var app = require('express')();
var http = require('http').Server(app);

and if I just use 'require('express')' without the second parenthesis it doesn't work. What does the second parenthesis do and where could I find documentation for the syntax?


Solution

  • The type returned from require('express') is a function. The second set of parenthesis is you actually invoking the function to create an instance of an express app.

    It's the equivalent of you doing

    const express = require('express');
    const app = express();