『Node.js 超入門』 一癖あるも良心的な入門書

Node.js に関する新しい書籍が出版されているのを書店で目にしたので購入して一読してみました。

本書の内容は書名どおり Node.js の導入とサンプル・アプリケーションの説明に重点が置かれており、スケーラブルなアプリケーションやデプロイメントについては触れられていません。

当然、Nginxとの連携やサーバ環境構築などについての言及もありません。

しかし、サンプル・アプリケーションではクッキーやセッションの使用、データベースとの連携など、アプリケーション作成において多くの人が知りたいと思う部分がきちんと記述されており、プログラムとして問題なく動作します。

Node.js に関する書籍の発行数が少ないこともあり、現行の環境でサンプル・アプリケーションが実際に動かして使える入門書の存在は貴重です。




本書では入門者を対象としていることから、Windows OS 上にてアプリケーションを実行する方法が書かれているのですが、他の OS でも全く問題なくアプリケーションを実行できます。

私の開発環境は下記の通り Ubuntu (開発環境として書籍では言及されていない OS) ですが、Visual Studio Code も含めて問題なく本書の開発環境を再現できました。

もっとも使い慣れたテキストエディタの方が作業効率が良いので VSC は、動作確認ぐらいしかしていないのですが。

しかし、せっかく VS Code を入れるのに ESLint でデバッグしないのでしょうか。これなら本当に Vim でいいじゃん。

DISTRIB_DESCRIPTION “Ubuntu 16.04.4 LTS”
js v8.11.1
node v8.11.1
npm 5.6.0
express 4.16.0
sqlite3 3.22.0
Visual Studio Code 1.22.2

なぜ SQLite3 がここに挙げられているのか? と思った方は、とても鋭い認識力をお持ちです。

本書のサンプルアプリケーションでは MongoDB や CouchDB といった JavaScript のデータベースではなく SQLite を用います。

入門者なら最初はSQL対応のDBを知るべきという著者の考え方が反映されている部分で、その主張自体はとても共感できるものですが、 Node.js を興味を持つ人で SQL に一切触れたことがない人はどれくらいいるのか判断が難しいところです。

個人的には NoSQL の独自クエリは大嫌いですし、サーバ接続の問題からアプリケーション起動に失敗する等の無用のトラブルを避けるという、入門書ならではの前提を考えると仕方がないかなと思えます。

またサンプルアプリケーションへのデータベースの導入時は、文字リテラルでクエリを手書きしていて閉口しますが、これも著者の考えあってのものであり、次章からは O/RM ツールが導入されますので安心してください (O/RMという用語自体は使われずにデータベース用のモジュールと記述されます)。

本書中では重要なポイントとされつつ、DBやSQLについては (Node.js という主題から外れるという事情もあって、詳しくは) 各自学習してみてくださいで終わってしまうので、本当に DB や SQL に触れたことがない人は他の書籍も参考にされた方が良いです。



SQL 第2版 ゼロからはじめるデータベース操作 (プログラミング学習シリーズ)


ここまで述べてきただけでも本書の役割や性格が見えてきたと思われるのですが、長所だけではなく短所ももちろんあります。

その中でも最も致命的なものは、サンプルアプリケーションのソースコードが目的のアプリケーションや章毎に整理されておらず、読み進めるごとに煩雑で読みづらくなっていくことです。

第2章で作成したアプリケーションを一部改編して第6章まで再利用していく構成になっており、進行時点においては既に使われていないモジュールがそのまま残されたり、必要以上にソースコードが複雑で読みづらくなります。

Node.js やフレームワークの初見での分かりづらさは、主にファイルや関数の数の多さと、(自分で設計したプログラムではないために) 「どの関数で何が行われているのか」を把握しづらい点にあります。

その点、本書では — 少なくとも初期の段階では — どこに何があり、どの関数が何が行われているのかという見通しがよく、それだけでも入門書として大きな価値があります。

それがソースコードの書き換えを行うごとに見通しが悪くなっていき、内容自体の複雑化と併せて終章に向かうに連れて苦痛になっていきます。

命名規則や開発効率の点からも良くありません。

私の場合はアプリケーションの目的が変わった時点で、プロジェクト (ディレクトリ/フォルダ) を新規に作成して対応しましたが、もし本書の中であらかじめサンプルアプリケーションやソースコードが整理されていて、個々のアプリケーションが単体でも実行可能であれば学習効率が大きく向上したことは間違いありません。

それでもアプリケーションとして、現行の環境できちんと動作するところは皮肉や嫌味ではなく特筆すべき点です。

例外処理を入れた方が良さそうと言った理由から個人的に書き換えたくなるところはありましたが、サンプルのソースコードをそのままアプリケーションとして実行してエラーとなったのは、一部の ejs ファイルの form method ただ1点だけです。

それも下記のようにパスを書き換えれば問題なく実行できます。

<form method="post" action="./edit">

サンプルが正しく実行できるという安心感はとても重要で、だからこそソースコードを自身で改編して試行錯誤しながら上手くいく方法を模索できます。

本書から学べることは本書に書いてあることだけではない訳で、自分で console.log を付け加えて変数の中身を調べたり、関数を付け加えてサンプルとの出力の違いを確認したり、データベースを MongoDB に置き換えても構わないわけです。

上述のように一癖ある本書ですが、 Node.js の貴重な入門書であり、他の関連書籍と内容があまり重複しないという点でも一読してみて良かったなと思えました。

追記: node.js の開発をやるなら nodemon を入れておくと頻繁に再起動しなくて済むので楽です。



Node.js超入門


Tubeless Ksyrium Elite UST – Quick & Easy to install a Tire! Quick & Easy to lose Air!

When it comes to bicycle components, I don’t usually like fancy items like aluminum spokes, titanium bolts and carbon laminated rims. Of course, they offer some clear performance advantages over their steel and alloy counterparts. Huge tensile strength, greater stiffness, less weight and so forth. Nevertheless, I place greater importance on durability and spare-part availability. Because I occasionally take my bike overseas. And I have never thought about adapting my road bikes to new tubeless technology.

But, on the other hand, I always wanted to try tubeless wheels and tires. A tubeless system requires no inner tube; therefore, it is structurally simpler than clincher and tubular systems. And it is also widely adapted in motorcycles and automobiles. It should also work fine with road bikes. Unfortunately, for me at least, things are not that easy. The overwhelming majority of existing tubeless-ready road wheels require tire sealant instead of the inner tube. Tire sealant never makes complex systems simple. It is not a magic wand but sealing liquid which may contain hazardous chemicals, become the cause of the imbalance and even seal the valve. I personally think sealant is absolutely disgusting.

As the saying goes, everything should be made as simple as possible, but not simpler. Hence, when I first saw Mavic’s 2018 Ksyrium Elite and Yksion Pro UST — the first complete everyday riding tubeless road wheel-tire system, I finally decided to give it a shot!





Mavic Ksyrium Elite UST Pair M-25 – Red

The Ksyrium wheels come equipped with tubeless Yksion Pro tires. They are supposed to be delivered ready for riding. All you need to do before going riding is inflate tires — unless the tires are not installed wrong way around. When I got ones directly from France, I found the rear tire fitted in wrong rotation direction. C’est la vie. I removed the tire and measured the weight of the rear wheel (with the air valve) and tire.

I also wanted to know the weight of the front but couldn’t remove the tire that was stuck to the rim. After several attempts, I gave up and weighed the wheel and tire combo. I wouldn’t like to cut the brand new tire in half. And as I checked the weight of the rear tire, I could guess the weight.

If you are not familiar with the metric system, they are approximately 1.97 lbs (895 g) ; 0.56 lbs (253 g) and 2.05 lbs (931 g). The front wheel must weigh around 1.5 lbs (680 g), while the rear is 1.97 lbs (895 g).

Thanks to the tire’s exclusive design for Mavic wheels, it was surprisingly easy to remove and reinstall. So was getting the beads seated. I was, of course, an absolute beginner and had no experience in setting up tubeless tires. In spite of my lack of knowledge and experience, it took only less than 5 minutes to remount the tire on the rim bed. Neither ordeal nor soapy water were required. And both the front and the rear succeeded in holding air! I couldn’t help but let out a yell of satisfaction.

I hoped my excitement would last long — preferably longer than a week. As soon as I stopped inflating the tire with air, the front started losing the pressure. It lost almost all the air in just one hour.

The rear seemed alright at first. It, however, deflated slowly overnight. La vie est dure! I ended up adding a tiny bit of sealant to the tires.

Apart from the setting up, I instantly liked the wheels and tires. They accelerate noticeably quick and roll very quietly, effortlessly and comfortably. They are definitely worth the price. Now I need to test more tubeless tires and find out a way to use tubeless tires without using tire sealants.

Jaybird X3 review

The Jaybird X3 are a pair of high-quality, sweat-proof, bluetooth earbuds. I recently purchased ones, when my MDR-AS600BT had lost its ability to hold a battery charge. I loved my splash-proof, wireless, in-ear headset but SONY stopped producing the headset a couple of years ago. I looked for an alternative and found the X3 attractive.

I spend over an hour listening to audiobooks everyday. I use the in-ear headphones for running, commuting, traveling and indoor bike training. I’m certainly not an audiophile. Not even a big music fan. But I can’t imagine what my life would be without workout headphones. I always am enthusiastic about learning new things. And I love learning by ear.

The X3 satisfy all my needs. And they are very easy to use. They have just 3 buttons; 2 volume control buttons and the power button. When you press the power button and turn the headphones on, they announce the battery status to you.

And when paired with a cell phone or portable audio/video player, they tell you that they are wirelessly connected with the device. You never need to take off the headphones to see whether they are powered on, paired and working.

The X3 sound fantastic as well. For the weight at least. Although they are only approximately 0.63 ounces, the X3 sound clearly in almost every condition. On an airplane, crowded street, bike rollers, snowy Bavarian Alps and in a tropical storm.




Even at lower levels of volumes, you still are able to listen to the sound. So I constantly carry the headphones with me and use them with a waterproof smartphone.

The X3, of course, have certain drawbacks too (like any other headphones). Their battery life is relatively short in comparison to some other bluetooth headphones. And I get random audio dropouts/skipping on my X3 headphones every now and then, while I am running outdoors with them.

Nonetheless, the problems could be avoided if you carry the X3 power charger adapter and stay away from other runners using bluetooth devices. Overall I am satisfied with my X3 headphones.


Jaybird X3 In-Ear Wireless Bluetooth Sports Headphones – Sweat-Proof – Universal Fit – 8 Hours Battery Life – Blackout

Pros:

  • Great sound
  • Good fit
  • Announcement
  • Attractive look
  • Fast & seamless BT pairing
  • Only approx. 0.63 oz

Cons:

  • Short battery life
  • Audio dropouts
  • Expensive price