Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var express = require('express'); var app = express(), What is express()?? is it a method or a constructor? Where does it come from

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

This is how we create an express application. But what is this 'express()'? Is it a method or a constructor? Where does it come from??

like image 671
sreesreenu Avatar asked Dec 22 '14 09:12

sreesreenu


People also ask

What is var Express require (' express ');?

var express = require('express'); => Requires the Express module just as you require other modules and and puts it in a variable. var app = express(); => Calls the express function "express()" and puts new Express application inside the app variable (to start a new Express application).

What does Express () method do?

The application will then return a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template. Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.)

What is req and res in Express?

The req object represents the HTTP request and has properties for the request query string, parameters, body, and HTTP headers. The res object represents the HTTP response that an Express app sends when it gets an HTTP request. In our case, we are sending a text Hello World whenever a request is made to the route / .

What is Express node?

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.


1 Answers

Is it a method or a constructor?

Neither; it's a function, although if you said "method" I don't think anyone would give you a hard time.

A method is a function attached to an object. In JavaScript, methods are just mostly functions that you reference via object properties. (Update: As of ES2015, if you use method syntax to create them, they're slightly more than that because they have access to super.)

A constructor, in JavaScript, is a function you call via the new operator. Even though other functions may create things, we don't typically call them "constructors" to avoid confusion. Sometimes they may be "creator" or "builder" functions.

Where does it come from?

ExpressJS is a NodeJS module; express is the name of the module, and also the name we typically give to the variable we use to refer to its main function in code such as what you quoted. NodeJS provides the require function, whose job is to load modules and give you access to their exports. (You don't have to call the variable express, you can do var foo = require('express'); and use foo instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.)

The default export of express is a bit unusual in that it's a function that also has properties on it that are also functions (methods). That's perfectly valid in JavaScript,¹ but fairly unusual in some other languages. That's why you can create an Application object via express(), but also use express.static(/*...*/) to set up serving static files.


¹ In fact, it's completely normal. Functions have a couple of standard methods by default: call, apply, and toString for instance.

like image 82
T.J. Crowder Avatar answered Oct 08 '22 11:10

T.J. Crowder