LoginSignup
2
3

More than 5 years have passed since last update.

Node.js の mocky を オレオレhttps で動かしてみる、その2

Posted at

オレオレhttps

Node.js の mocky を使用し スタブREST API を作成してみる、その1 で、https 対応できないかなと調べたらさっくりできたのでそのメモ

デジタル証明書は、オレオレ証明書を作るのでそんなわけで、オレオレhttpsつーこと

オレオレ証明書作成方法

作り方は簡単、質問されるけどまあ適当に答えて問題ないです。あと有効期限は、10年で作成してます。
ちなみに openssl をインストールしておくこと。

$ openssl genrsa -out key.pem 1024
$ openssl req -new -key key.pem -out csr.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Kanda
Organization Name (eg, company) [Default Company Ltd]:hoge
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:メールアドレス

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

有効期限を10年で作成する。

$ openssl x509 -req -in csr.pem -signkey key.pem -out cert.pem

作成した証明書を mocky にコピー

作成した cert.pem, csr.pem を node_modules/mocky/lib にコピーします。

$ cd ./node_modules/mocky/lib
$ cp ../../../*.pem .

オリジナルの mocky.js をバックアップしておきます(httpで使うときのために)。そしてちょびっと追加するのと修正します。

$ cd ./node_modules/mocky/lib
$ cp mocky.js mocky.js.old
$ diff mocky.js mocky.js.old
5,11d4
< var fs = require('fs');
< 
< //
< var options = { 
<     key: fs.readFileSync('key.pem'),
<     cert: fs.readFileSync('cert.pem')
< };
20c13
<               srvType = params.srvType || 'https',
---
>               srvType = params.srvType || 'http',
52c45
<       return require(srvType).createServer(options, function(req, res) {
---
>       return require(srvType).createServer(function(req, res) {

mocky_sample4.js を実行してみる

Node.js の mocky を使用し スタブREST API を作成してみる、その1 で作成した mocky_sample4.js を起動してみます。

$ node mocky_sample4.js

特にエラーもでないですねw

curl で、オレオレhttps(無視するんだけどね)としてアクセスしてみます。

$  curl "https://127.0.0.1:4321/someurl4?a=999&b=777&kj=44Gv44GS56a/44OP44Ky" -k --verbose

okうまくいった。

参考サイト

Node, iOS, Android, HTML5, JavaScriptの話題。ときどきMacやLinux。
オレオレ証明書をopensslで作る(詳細版)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3