エクスプレス = require('エクスプレス')

Feb 01 2023
これは、この声明の舞台裏で何が起こっているのかを理解しようとしていたときのプロセスの仕組みです。さて、ボックスを見てみましょう ;) まず、require で実際に何が表現されているかを知ることから始めましょう。
Unsplash の Kelli McClintock による写真

これは、この声明の舞台裏で何が起こっているのかを理解しようとしていたときのプロセスの仕組みです。さて、箱を見てみましょう;)

まず、 requireで実際に何が表現されているかを知ることから始めましょう。

Express は、以下を使用したときにインストールしたフレームワーク パッケージです。

npm istall express

ここでわかるように、いくつかの関数とその他のものを定義しており、関数を含むいくつかのものをエクスポートしています。

輸出の意味がわからない?

ここでの輸出は、実際の商品の輸出に似ており、物を別の場所に移動して、そこで使用できるようにするだけです。

Ok?

JS では、次を使用して関数、プロパティ、その他のものを別のファイルにエクスポートできます。

module.exports = (stuff you want to export)

.js ファイルには、次の SayHi 関数があります。

// a.js

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

上で述べた行を追加すると、次のようになります。

// a.js

module.exports = sayHi

// a.js

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

いいえ、使用するにはインポートする必要があります。それは論理です。

しかし、それではどうやって?

以下の行を使用します。これは表現力豊かです。そのモジュールが必要なので、それを要求します。

require(module location);

// b.js

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

これで関数への参照ができたので、その参照に () を追加することで関数を呼び出すことができます。

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

これを見ることができます:

//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は単にmodule.exportsへの参照であるため、module.exports.jsonと言う代わりに、このショートカットを直接exports.jsonにすることができます。

大丈夫?

サーバーを作成し、ルーティングを追加したapp.jsファイルに移動しましょう。ここで確認する必要があるのは最初の行だけです。

// app.js

const express = require('express')

ログに記録して試してみましょう:

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

これは createApplication なので、実際に返されるのは createApplication() への参照です。

あなたも私と同じように、理解する前にそう疑問に思っているかもしれません。

上で見た他のエクスポートはどこにあるのでしょうか。:

// express.js module

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

それで、ここで何が起こっているのでしょうか?

それはすべて JS で動作する関数に関するものであり、オブジェクトのように動作します ;)

これを使用したとき:

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’);

__

このスニペットを見て、以下の各行で何が表現に相当するのかを自問してテストしてください。

// app.js

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

終了した?さあ行きましょう:

最初の行: 他のプロパティを含む createApplication 参照を返し、その参照を Express 定数に保存するモジュールを必要としました。

2 行目: 参照 Express に () を追加してこの Express 関数を呼び出し、その関数 (createApplication) はアプリ定数に格納したアプリ オブジェクトを返します。

3 行目では、ここで呼び出した静的関数のようないくつかのプロパティを持つ createApplication 関数を参照する Express 定数を使用しました。

さて、終わりました…