Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9042

nodejs SSLサーバ構築 チートシート

$
0
0

説明はこちら
nodejs サーバ構築 チートシート

node SSLサーバ チートシート

構成

├ server.js
├ package.json # nodejs サーバ構築 チートシートと同じ
├ download.txt # ダウンロード用ファイル。中身はなんでもok
└ cert
 ├ server.cnf
 ├ server.crt
 └ server.key

server.js
'use strict';varlog4js=require('log4js');varlogger=log4js.getLogger('test');log4js.configure({appenders:{default:{type:"console",layout:{type:"basic"}}},categories:{default:{appenders:['default'],level:'debug'}},replaceConsole:true,level:'info'})varexpress=require('express');varapp=express();varbodyParser=require('body-parser');app.use(bodyParser.json());app.use(bodyParser.urlencoded({extended:false}));varhttps=require('https');varfs=require('fs');varoptions={key:fs.readFileSync(__dirname+'/cert/server.key'),cert:fs.readFileSync(__dirname+'/cert/server.crt')};varutil=require('util');varhost='localhost';varport=8443;app.use(function(req,res,next){logger.info('%s %s',req.method,req.url);logger.info('header='+JSON.stringify(req.headers));logger.info('query='+JSON.stringify(req.query));logger.info('body='+JSON.stringify(req.body));next();});varserver=https.createServer(options,app).listen(port,function(){});console.log('*** server start https://%s:%s ***',host,port);logger.info('*** server start https://%s:%s ***',host,port);server.timeout=30000;process.env['NODE_TLS_REJECT_UNAUTHORIZED']=0;// オレオレ証明書サーバへリクエストする場合// top pageapp.all('/',function(req,res){// methodを限定しないres.header({'Content-type':'text/html'});res.end(util.format('<h1>http://%s:%s for honya</h1>',host,port));// サーバの用途が分かるようなタイトルを表示する});// json を返す場合 REST形式app.all('/rest/v1/user/:id',function(req,res){logger.info('id='+req.params.id);// url パラメータの参照letjson={id:req.params.id,name:"honya",dob:(newDate('2020-01-01 12:34:56')).getTime(),}res.status(201).json(json);});// html を返す場合 ASP.NET形式app.all('/asp/honya.asmx',function(req,res){lethtml=`
<html>
<body>
    <h1>request url: ${req.method}${req.url}</h1>
    <div>response</div>
</body>
</html>
`;res.end(html);});// 存在するファイルをダウンロードする場合 cgi形式app.all('/cgi-bin/honya.cgi',function(req,res){res.download(__dirname+'/download.txt','server.log');});// csvデータをダウンロードする場合 php形式app.all('/php/honya.php',function(req,res){letheader={'Content-disposition':'attachment; filename=server.csv','Content-type':'text/plain; charset=utf-8',}res.header(header);letcsv="id,name\n1,honya\n";res.send(csv);});
server.cnf
[req]distinguished_name=req_distinguished_namex509_extensions=SANprompt=no[req_distinguished_name]C=Country initials like US, RO, GEST=StateL=LocationO=Organization NameOU=Organizational Unit CN=localhost[SAN]keyUsage=critical, digitalSignature, keyAgreementextendedKeyUsage=serverAuthsubjectAltName=@alt_names[alt_names]DNS.1=localhost

server.crt と server.key はコマンドで作成

cd ~/cert # 証明書フォルダへ移動
openssl req \-newkey     rsa:4096 \-keyout     server.key \-x509\-nodes\-out        server.crt \-subj"//CN=localhost"\-days       3650 \-reqexts    SAN \-extensions SAN \-config     server.cnf

起動

nodejs サーバ構築 チートシートと同じ

常時起動

nodejs サーバ構築 チートシートと同じ

参考

証明書の取り込みは以下のStep 6.を参照
create a trusted self-signed SSL cert for localhost
※取り込んだあと、ブラウザの再起動をお忘れなく σ(゚∀゚ )オレ


Viewing all articles
Browse latest Browse all 9042

Trending Articles