Reputation: 47
This is a very newbie Node/express question. It has been bugging me for a couple of weeks now. I know what this does, and I've been able to get past it and build some stuff using Express, but I cannot wrap my head around the logic used in the line 'const app = express()'. I don't think I have seen this before in javascript. In this case, express is an object, not a function right? And this line is what gives the variable 'app' access to lots of important methods like 'listen' and 'get'. But doesn't the syntax here make it seem that the value - express() - is a function? What exactly is happening here.
Upvotes: 0
Views: 584
Reputation: 101
Good question,
we have this:
const express = require("express"),
const app = express()
"The express constant is still used for some Express.js tools related to configuring your application. app is used mainly for anything created for the application’s movement of data and user interaction."
Reference: Jonathan Wexler. Get Programming with Node.js (Kindle Locations 2619-2621). Manning Publications.
From my understanding, const express is a method required from the express module.
const express = require("express")
You then use express() method to instantiate a web server named "const app"
const app = express()
So the const app is really a web server object made by express.js developers that have abstracted 2 main core functionalities:
so app is an object that enables you the developer to not have to worry on mapping browser requests with proper handle callbacks, and also to not have to create the webserver.
Plus you get in a bunch of new methods for app that allow you to do different things with your app. You also get a bunch of methods when defining requests, and responses. Like res.send()
e.g. browser does a "GET" to "/about" and app is listening and receives the request (abstracted in app object) then executes the call back (thanks maybe to the app.listen() method??) based on it's abstracted map
Hope that made sense.
Upvotes: 0
Reputation: 708036
In the line of code:
const app = express();
express
is a function. If you do typeof express
, it will tell you that it's a function.
When you call that function, it returns a newly created object so it's a function acting like a "factory function". It's kind of like a constructor, but instead of you using new
with it, the creation of the object is contained inside the function. The function creates the new object, initializes it and returns it.
What may be confusing you is that express
also has properties such as:
express.static(...)
express.json(...)
In Javascript, a function is also an object (you can think of a function as derived from an object) and can contain properties. So, while express
is a factory function, it can also have properties that can be used independently.
In this case, express is an object, not a function right?
Yes and no, express
is a function. Functions are derived from object
and can also have properties. So, in a way, you can say it's both a function and an object and you can use it as either. In object-oriented terms, object is the base class and function is the derived class so a function has the capabilities of both a function and an object.
And this line is what gives the variable 'app' access to lots of important methods like 'listen' and 'get'.
The line const app = express();
creates an app object that has it's own methods such as app.get()
and app.post()
that allow you to register route handlers. And, a method like app.listen()
allows you to start the server. There are many methods on the app
object.
But doesn't the syntax here make it seem that the value - express() - is a function?
Yes, it is a function. And, functions in Javascript can also have properties.
Upvotes: 3