express = require('express')

Feb 01 2023
So funktioniert der Prozess, als ich versuchte zu verstehen, was sich hinter den Kulissen dieser Aussage abspielte. Werfen wir nun einen Blick auf die Box ;) Beginnen wir zunächst damit, herauszufinden, was in require tatsächlich ausgedrückt wird.
Foto von Kelli McClintock auf Unsplash

So funktioniert der Prozess, als ich versuchte zu verstehen, was sich hinter den Kulissen dieser Aussage abspielte. Werfen wir nun einen Blick auf die Box ;)

Beginnen wir zunächst damit, herauszufinden, was in require tatsächlich ausgedrückt wird .

express ist das Framework-Paket, das Sie installiert haben, als Sie Folgendes verwendet haben:

npm istall express

Wie Sie hier sehen, haben wir einige definierte Funktionen und andere Dinge, und dort exportieren wir einige Dinge, einschließlich Funktionen.

Sie wissen nicht, was mit Exportieren gemeint ist?

Der Export hier ist wie der Export von Waren im wirklichen Leben, man transportiert lediglich Dinge an einen anderen Ort, um sie dort verwenden zu können;

OK?

In JS können Sie Funktionen, Eigenschaften und andere Dinge in eine andere Datei exportieren, indem Sie Folgendes verwenden:

module.exports = (stuff you want to export)

In einer.js-Datei haben wir diese sayHi-Funktion:

// a.js

const sayHi = () => { return 'Hi' }

Durch das Hinzufügen dieser Zeile haben wir oben angegeben:

// a.js

module.exports = sayHi

// a.js

const sayHi = () => { return 'Hi' }
module.exports = sayHi

Nein, wir müssen es importieren, um es verwenden zu können! Es ist Logik.

aber wie dann?

Verwenden Sie die folgende Zeile. Es ist ausdrucksstark: Wir brauchen dieses Modul, also brauchen wir es.

require(module location);

// b.js

const sayHi = require('./a.js');

Jetzt haben wir eine Referenz auf die Funktion und können sie aufrufen, indem wir () zur Referenz hinzufügen;

// b.js
const sayHi = require('./a.js');
console.log( sayHi() ) //output: Hi

wir können das sehen:

//express.js module

exports = module.exports = createApplication;

//express.js module 

exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.raw

exports ist hier nur ein Verweis auf module.exports, sodass Sie anstelle von module.exports.json einfach diese Verknüpfung direkt exports.json verwenden können.

In Ordnung?

Gehen wir zur Datei app.js , in der wir unseren Server erstellt und etwas Routing hinzugefügt haben. Jetzt müssen wir nur noch die erste Zeile sehen:

// app.js

const express = require('express')

Versuchen wir es, indem wir es protokollieren:

// app.js

const express = require('express')

console.log(express)


[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]
}

Es handelt sich um „createApplication“. Was zurückgegeben wurde, ist also eigentlich der Verweis auf „createApplication();“

Sie fragen vielleicht wie ich, bevor ich es verstanden habe!

Wo sind die anderen Exporte, die wir oben gesehen haben? :

// express.js module

exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.raw

Also, was passiert hier?

Es geht um das Verhalten von Funktionen in JS. Es verhält sich wie ein Objekt ;)

als du das benutzt hast:

exports = module.exports = createApplication;

exports.static = require('serve-static');
exports.json = bodyParser.json
exports.query = require('./middleware/query');
exports.raw = bodyParser.raw

exports = module.exports = createApplication;

createApplication.static = require(‘serve-static’);

__

Schauen Sie sich dieses Snippet an und testen Sie sich selbst, indem Sie fragen, was in den einzelnen Zeilen unten „express“ bedeutet:

// app.js

const express = require('express')
const app = express();
app.use(express.static('public'))

fertig? jetzt aber los:

In der ersten Zeile: Wir benötigten das Modul, das eine createApplication-Referenz zurückgegeben hat, die andere Eigenschaften enthält, und haben diese Referenz in der Express-Konstante gespeichert.

In der zweiten Zeile: Wir haben diese Express-Funktion aufgerufen, indem wir () zur Referenz Express hinzugefügt haben, und diese Funktion (createApplication) gibt ein App-Objekt zurück, das wir in der App-Konstante gespeichert haben.

In der dritten Zeile: Wir haben die Express-Konstante verwendet, die sich auf die Funktion „createApplication“ bezieht, die einige Eigenschaften hat, wie z. B. die statische Funktion, die wir hier aufgerufen haben.

Jetzt bin ich fertig...