Nodejs+socket.io+express+https API化 その1

javascript

呼び出し方法をGoogleMapAPIライクにしたときに、URLに含まれる”?”文字の処理に困ったときの備忘録です。

呼び出し方法

<script src="https://###.####.##/anonymous-chat/v1/api/js?key=%%%%%%%;callback=initChat"></script>

app.get

app.get('/anonymous-chat/v1/:param1/:param2', (req, res) => {
  console.log('param1='+req.params['param1'] );
  console.log('param2='+req.params['param2'] );
  var url = require('url').parse(req.url);
  console.log('href='+url.href);
  console.log('protocol='+url.protocol);
  console.log('hostname='+url.hostname);
  console.log('path='+url.path);
  console.log('query='+url.query);

  console.log('method='+req.method);
  console.log('headers='+req.headers['host']);
  console.log('conection='+req.headers['connection']);
  console.log('cache='+req.headers['cache-control']);
  console.log('accept='+req.headers['accept']);
  console.log('upgrade-insecure-requests='+req.headers['upgrade-insecure-requests']);
  console.log('user-agent='+req.headers['user-agent']);
  console.log('accept-encoding='+req.headers['accept-encoding']);
  console.log('accept-language='+req.headers['accept-language']);
}

出力結果

param1=api
param2=js
href=anonymous-chat/api/js?key=%%%%%%%;callback=initChat
protocol=null
hostname=null
path=anonymous-chat/api/js?key=%%%%%%%;callback=initChat
query=key=%%%%%%%;callback=initChat
method=GET
headers=###.####.##:$$$$
conection=close
cache=undefine
accept=*/*
upgrade-insecure-requests=undefine
user-agent='Mozilla/5.0 (Windows NT 10.0;,\........省略
accept-encoding=zip, deflate, br
accept-language=ja,en-US;q=0.7,en;q=0.3

コメント