環境構築
Node.js + Expressの構成でサーバープロセスが起動するように環境を構築する。 なおNode.jsは既にインストール済みとする。
まずは初期化する。
npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (1645543138)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /opt/ng/symdon/pages/posts/1645543138/package.json:
{
"name": "1645543138",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes) yes
Expressをインストールする。
npm install --save expressファイルを作成する
src/app.js
Expressのエントリーポイントを記述したファイルを作成する。
const express = require('express')
const { getName } = require('./foo')
const GetRequestHeaders = require('./GetRequestHeaders')
const app = express()
const port = 3000
app.use('/request-headers', GetRequestHeaders)
app.get('/', (req, res) => {
res.send(getName())
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})src/foo.js
app.js以外のモジュールも追加しておきたいのでfoo.jsを作っておく。
const getName = () => {
return 'testing';
};
exports.getName = getName;その他のファイル
src/GetRequestHeaders.js を以降の項目で作成している。
実行
nodeコマンドを使って起動する。
node src/app.jsnode src/foo.jsプロセスが起動すると3000のTCPポートを待ち受ける。
Example app listening on port 3000ブラウザでアクセスするとこのような文字列が表示される。

Debuggerで処理を停止しステップ実行する
node inspect コマンドを用いてデバッガを起動できる。
node inspect src/app.js(node:50878) ExperimentalWarning: AbortController is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
< Debugger listening on ws://127.0.0.1:9229/7dd75c98-e2b1-4fdc-a2e0-185f631efdf8
< For help, see: https://nodejs.org/en/docs/inspector
<
< Debugger attached.
<
ok
Break on start in src/app.js:1
> 1 const express = require('express')
2 const { getName } = require('./foo')
3
debug> c
< Example app listening on port 3000
<ファイルパスと行番号を指定してbreakpointを設定する。
debug> sb("/opt/ng/symdon/pages/posts/1645543138/src/foo.js", 2)
1 const getName = () => {
> 2 return 'testing';
3 };
4
5 exports.getName = getName;
6
7
debug>ブラウザでアクセスすると、このbreakpointで停止した状態で入力待ちとなる。
break in src/foo.js:2
1 const getName = () => {
> 2 return 'testing';
3 };
4
debug> n
break in src/foo.js:2
1 const getName = () => {
> 2 return 'testing';
3 };
4
debug> c
debug>HTTPリクエストヘッダーを取得する
リクエストヘッダーにはリクエストオブジェクトのheaders属性でアクセスできる。 ここではUser-Agentを取得し、レスポンスでそれを応答する例を示す。
プロセスを再起動しリクエストを送信する。 リクエストのUser-AgentにはEmacsを指定した。
GET http://localhost:3000/request-headers
User-Agent: EmacsYour user agent is "Emacs".
// GET http://localhost:3000/request-headers
// HTTP/1.1 200 OK
// X-Powered-By: Express
// Date: Sat, 26 Feb 2022 23:45:38 GMT
// Connection: keep-alive
// Keep-Alive: timeout=5
// Content-Length: 27
// Request duration: 0.010399s
User-Agentの値を取得し、期待通りにレスポンスにUser-Agentの値を返していることが確認できた。