express = require(‘express’)
This is how the process works as I was trying to understand what was going on behind the scenes of this statement. Now, let’s take a look at the box ;)
let’s first start by knowing what is actually express in require.
express is the framework package you installed when you used:
npm istall expressOk, let’s take a look at that package here, open it;
As you see here we have some defined functions and other stuff, and there we are exporting some stuff including functions.
you don’t know what is meant by exporting?
Exporting here is like exporting goods in real life, you are just moving stuff to other place to be able to use them there ;
Ok?
In JS you can export functions, properties and other stuff to another file using:
module.exports = (stuff you want to export)To illustrate this, assume you have two files a.js and b.js:
In a.js file we have this sayHi function:
// a.js
const sayHi = () => { return 'Hi' }Now we want to export this function to be able to use it in another file like b.js, guess how?
By adding that line we stated above which is:
// a.js
module.exports = sayHiso our a.js file after updating includes:
// a.js
const sayHi = () => { return 'Hi' }
module.exports = sayHiNow in b.js file, can we use it directly?
No, we must import it, to be able to use! it’s logic.
but how then?
Using the line below. It is expressive: we need that module so we require it.
require(module location);Now in b.js file we write the line to receive the function , actually reference to the function and store it using a constant:
// b.js
const sayHi = require('./a.js');we can name the constant any name we want ;
Now, we have a reference to the function and can call it by adding () to the reference;
// b.js
const sayHi = require('./a.js');
console.log( sayHi() ) //output: HiNow we have an idea about exporting and importing modules lets jump into our express module (the link is above):
we can see this:
//express.js module
exports = module.exports = createApplication;and some other lines of exporting like this:
//express.js module
exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.rawyou may ask, what is exports keyword, we stated only module.exports?
Get Hisham Elmorsi’s stories in your inbox
Join Medium for free to get updates from this writer.
exports here is just a reference to module.exports so that instead of saying module.exports.json you can just this shortcut directly exports.json.
Alright?
let’s go to the app.js file where we created our server and added some routing, we need now to see only the first line:
// app.js
const express = require('express')here we are importing the module, Do you know what express is evaluated to right now?
let’s try by logging it:
// app.js
const express = require('express')
console.log(express)And here is the result in console:
[Function: createApplication] {
application: {
init: [Function: init],
defaultConfiguration: [Function: defaultConfiguration],
lazyrouter: [Function: lazyrouter],
handle: [Function: handle],
use: [Function: use],
route: [Function: route],
engine: [Function: engine],
param: [Function: param],
set: [Function: set],
path: [Function: path],
enabled: [Function: enabled],
disabled: [Function: disabled],
enable: [Function: enable],
disable: [Function: disable],
acl: [Function (anonymous)],
bind: [Function (anonymous)],
checkout: [Function (anonymous)],
connect: [Function (anonymous)],
copy: [Function (anonymous)],
delete: [Function (anonymous)],
get: [Function (anonymous)],
head: [Function (anonymous)],
link: [Function (anonymous)],
lock: [Function (anonymous)],
'm-search': [Function (anonymous)],
merge: [Function (anonymous)],
mkactivity: [Function (anonymous)],
mkcalendar: [Function (anonymous)],
mkcol: [Function (anonymous)],
move: [Function (anonymous)],
notify: [Function (anonymous)],
options: [Function (anonymous)],
patch: [Function (anonymous)],
post: [Function (anonymous)],
propfind: [Function (anonymous)],
proppatch: [Function (anonymous)],
purge: [Function (anonymous)],
put: [Function (anonymous)],
rebind: [Function (anonymous)],
report: [Function (anonymous)],
search: [Function (anonymous)],
source: [Function (anonymous)],
subscribe: [Function (anonymous)],
trace: [Function (anonymous)],
unbind: [Function (anonymous)],
unlink: [Function (anonymous)],
unlock: [Function (anonymous)],
unsubscribe: [Function (anonymous)],
all: [Function: all],
del: [Function (anonymous)],
render: [Function: render],
listen: [Function: listen]
},
request: IncomingMessage {
header: [Function: header],
get: [Function: header],
accepts: [Function (anonymous)],
acceptsEncodings: [Function (anonymous)],
acceptsEncoding: [Function (anonymous)],
acceptsCharsets: [Function (anonymous)],
acceptsCharset: [Function (anonymous)],
acceptsLanguages: [Function (anonymous)],
acceptsLanguage: [Function (anonymous)],
range: [Function: range],
param: [Function: param],
is: [Function: is],
protocol: [Getter],
secure: [Getter],
ip: [Getter],
ips: [Getter],
subdomains: [Getter],
path: [Getter],
hostname: [Getter],
host: [Getter],
fresh: [Getter],
stale: [Getter],
xhr: [Getter]
},
response: ServerResponse {
status: [Function: status],
links: [Function (anonymous)],
send: [Function: send],
json: [Function: json],
jsonp: [Function: jsonp],
sendStatus: [Function: sendStatus],
sendFile: [Function: sendFile],
sendfile: [Function (anonymous)],
download: [Function: download],
type: [Function: contentType],
contentType: [Function: contentType],
format: [Function (anonymous)],
attachment: [Function: attachment],
append: [Function: append],
header: [Function: header],
set: [Function: header],
get: [Function (anonymous)],
clearCookie: [Function: clearCookie],
cookie: [Function (anonymous)],
location: [Function: location],
redirect: [Function: redirect],
vary: [Function (anonymous)],
render: [Function: render]
},
Route: [Function: Route],
Router: [Function (anonymous)] {
param: [Function: param],
handle: [Function: handle],
process_params: [Function: process_params],
use: [Function: use],
route: [Function: route],
acl: [Function (anonymous)],
bind: [Function (anonymous)],
checkout: [Function (anonymous)],
connect: [Function (anonymous)],
copy: [Function (anonymous)],
delete: [Function (anonymous)],
get: [Function (anonymous)],
head: [Function (anonymous)],
link: [Function (anonymous)],
lock: [Function (anonymous)],
'm-search': [Function (anonymous)],
merge: [Function (anonymous)],
mkactivity: [Function (anonymous)],
mkcalendar: [Function (anonymous)],
mkcol: [Function (anonymous)],
move: [Function (anonymous)],
notify: [Function (anonymous)],
options: [Function (anonymous)],
patch: [Function (anonymous)],
post: [Function (anonymous)],
propfind: [Function (anonymous)],
proppatch: [Function (anonymous)],
purge: [Function (anonymous)],
put: [Function (anonymous)],
rebind: [Function (anonymous)],
report: [Function (anonymous)],
search: [Function (anonymous)],
source: [Function (anonymous)],
subscribe: [Function (anonymous)],
trace: [Function (anonymous)],
unbind: [Function (anonymous)],
unlink: [Function (anonymous)],
unlock: [Function (anonymous)],
unsubscribe: [Function (anonymous)],
all: [Function (anonymous)]
},
json: [Function: json],
query: [Function: query],
raw: [Function: raw],
static: [Function: serveStatic] {
mime: Mime {
types: [Object: null prototype],
extensions: [Object: null prototype],
default_type: 'application/octet-stream',
Mime: [Function: Mime],
charsets: [Object]
}
},
text: [Function: text],
urlencoded: [Function: urlencoded]
}Have you noticed something you saw before, just go up!
It’s createApplication, so what was returned is actually the reference to the createApplication();
You may be asking like me, before I understood!
where are the other exports we saw above like this! :
// express.js module
exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.rawTry to search this output for an export you saw like static, you will find it as a property:
so what is happening here?
It’s all about functions behaving in JS, It behaves like an object ;)
when you used this:
exports = module.exports = createApplication;you are exporting createApplication, and when you used these lines:
exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.rawyou were actually adding those properties to createApplication function reference, as exports and modules.exports reference to createApplication from this line:
exports = module.exports = createApplication;so this line : exports.static = require(‘serve-static’) is running as if it was:
createApplication.static = require(‘serve-static’);Now you understand what is happening.
__
Take a look at this snippet and test yourself by asking what is express equivalent to in each line below:
// app.js
const express = require('express')
const app = express();
app.use(express.static('public'))Don’t look at the answers before trying yourself ;)
finished? now lets go:
At the first line: we required the module that returned a createApplication reference which includes other properties and stored that reference at express constant;
At the second line: we called this express function by adding () to the reference express and that function (createApplication) returns app object which we stored in app constant.
At the third line: we used express constant which refers to createApplication function that has some properties like static function that we called here.
Now, I finished…








