Express:req.socketとreq.connectionプロパティでクライアントIPアドレスを取得する
Node.js、Express、URL:ExpressでフルURLを取得する方法
Node.jsのExpressフレームワークで、リクエストされたURL全体を取得するにはいくつかの方法があります。
方法
- req.url プロパティ:
最も簡単な方法は、req.url
プロパティを使用することです。これは、リクエストされたURLのパス部分のみを返します。
app.get('/', (req, res) => {
console.log(req.url); // '/'を出力
res.send('Hello World!');
});
- req.originalUrl プロパティ:
req.originalUrl
プロパティは、プロキシサーバーの後ろにある場合でも、元のリクエストされたURL全体を返します。
app.get('/', (req, res) => {
console.log(req.originalUrl); // '/?proxy=true'を出力
res.send('Hello World!');
});
- req.headers.host プロパティと req.headers.protocol プロパティ:
app.get('/', (req, res) => {
const fullUrl = `${req.headers.protocol}://${req.headers.host}${req.url}`;
console.log(fullUrl); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
- URLモジュール:
URL
モジュールを使用して、リクエストされたURLを解析し、個々のコンポーネントにアクセスすることもできます。
const url = require('url');
app.get('/', (req, res) => {
const parsedUrl = url.parse(req.url);
console.log(parsedUrl.href); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
ExpressでフルURLを取得するには、いくつかの方法があります。どの方法を使用するかは、要件と好みによって異なります。
const express = require('express');
const app = express();
// req.url プロパティ
app.get('/', (req, res) => {
console.log(req.url); // '/'を出力
res.send('Hello World!');
});
// req.originalUrl プロパティ
app.get('/proxy', (req, res) => {
console.log(req.originalUrl); // '/?proxy=true'を出力
res.send('Hello World!');
});
// req.headers.host プロパティと req.headers.protocol プロパティ
app.get('/headers', (req, res) => {
const fullUrl = `${req.headers.protocol}://${req.headers.host}${req.url}`;
console.log(fullUrl); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
// URLモジュール
app.get('/url', (req, res) => {
const url = require('url');
const parsedUrl = url.parse(req.url);
console.log(parsedUrl.href); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
app.listen(3000);
このコードを実行すると、ブラウザで http://localhost:3000/
にアクセスすると、コンソールに以下の出力が表示されます。
/
/?proxy=true
https://localhost:3000/
https://localhost:3000/
上記以外にも、req.get('host')
や req.get('protocol')
などのプロパティを使用して、フルURLを取得することもできます。
詳細は、Express.js ドキュメント - Request Object を参照してください。
ExpressでフルURLを取得する他の方法
req.protocol プロパティと req.get('host') プロパティ:
app.get('/', (req, res) => {
const fullUrl = `${req.protocol}://${req.get('host')}${req.url}`;
console.log(fullUrl); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
req.headers['x-forwarded-proto'] プロパティ:
プロキシサーバーを使用している場合は、req.headers['x-forwarded-proto']
プロパティを使用して、元のプロトコルを取得できます。
app.get('/', (req, res) => {
const protocol = req.headers['x-forwarded-proto'] || req.protocol;
const fullUrl = `${protocol}://${req.get('host')}${req.url}`;
console.log(fullUrl); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
req.hostname
プロパティは、リクエストされたホスト名のみを返します。
app.get('/', (req, res) => {
const fullUrl = `https://${req.hostname}${req.url}`;
console.log(fullUrl); // 'https://localhost:3000/'を出力
res.send('Hello World!');
});
req.socket.remoteAddress
プロパティは、クライアントのIPアドレスを返します。
app.get('/', (req, res) => {
const fullUrl = `https://${req.socket.remoteAddress}${req.url}`;
console.log(fullUrl); // 'https://127.0.0.1:3000/'を出力
res.send('Hello World!');
});
req.connection.remoteAddress
プロパティは、req.socket.remoteAddress
と同じように機能します。
app.get('/', (req, res) => {
const fullUrl = `https://${req.connection.remoteAddress}${req.url}`;
console.log(fullUrl); // 'https://127.0.0.1:3000/'を出力
res.send('Hello World!');
});
node.js url express