Diary 2018-08-17 (tardiness) nodejs day
Today's workings
- HTTP2 の勉強
- swagger-node と格闘
Details
1. HTTP2 の勉強
@ariaki4dev さんのスライドはいつも本当に勉強になるなぁ。
TLS1.3
は最近リリースされたんだったな。そして、 IPv6
どうなったの?
2. swagger-node
と格闘
やったことはこんな感じ。
swagger-node
をグローバルインストールし、swagger project create hoge
で新規にプロジェクト作成。- 初期状態だと
app.js
にルーティング設定を書くことになるので修正
./app.js
'use strict'; const SwaggerExpress = require('swagger-express-mw') - const app = require('express')() + const express = require('express') + const app = express() + const routes = require('./routes') module.exports = app; // for testing - const config = { - appRoot: __dirname // required config - } // 今回は静的ファイルを扱うので + app.use(express.static('public')) + app.use(routes()) (途中省略) - if (swaggerExpress.runner.swagger.paths['/hello']) { - console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott') - }
app.js
で読み込むroutes.js
を別途作成
./routes.js
const express = require('express') // contollers const hogeController = require('./api/controllers/hoge') module.exports = (router = express.Router()) => { // hoge endpoints router.post('/api/hoge', hogeController.post) router.get('/api/hoge', hogeController.get) router.put('/api/hoge', hogeController.put) return router }
hogeController
用のhoge.js
を作成
'use strict' module.exports = { post: post, get: get, put: put } const get = (req, res) => { const data = { name: "kuwahara", twitter: "kuwhara_jsri", } res.stastus(200) res.json(data) } const post = (req, res) => { /* ... */ } const put = (req, res) => { /* ... */ }
とりあえず、上記で正常系のモック環境は作れた。ただし、モックデータなどは api/mocks/
ディレクトリにて書くんだとは思うが、
そこまで細かいものを求めてはいなく、チームのメンバーが開発時に叩けるモックを用意するだけで良いので今回はやらない。
ただし、データセットなどは別ファイルに切り出して、それを読み込もうかなとは思っている。後は、4xx, 5xx
エラー時のレスポンスなどの設定が必要で、これは共通処理なので、パラメータでエラーコードとメッセージを渡すようにすれば良い。
Comments
まずは遅刻してもうたのはいかん。最近はフロントエンドのお仕事をしているのだが、やっぱり nodejs
好きだわ。
すべての処理が非同期ってのはちょっと面倒だと思うこともあるし、ひたすら Promise
や Async/Await
を駆使することになるが、
その設計を考えるのが物凄く楽しい。みんなも nodejs 書いてみてほしいなど。
ではでは(=゚ω゚)ノ