Intel EdsionでNode.jsのサーバを動かす

続いて、Node.js + expressでWebサーバを立ち上げてみます

パッケージマネージャの更新

Yoctoではapt-getとかではなく、opkgというパッケージマネージャを使うようです。これを最新にしておきます。 こちらを参考に

# cd /etc/opkg
# curl http://nonnoise.github.io/Edison/_sources/Edison/base-feeds.conf 
# curl http://nonnoise.github.io/Edison/_sources/Edison/intel-iotdk.conf 
# curl http://nonnoise.github.io/Edison/_sources/Edison/mraa-upm.conf

アップデートしておきます

# opkg update
# opkg upgrade

Node.jsの確認

とりあえずコマンド叩いてみましょう

# node
>
(^C again to quit)
>

間違いなくNode.jsが入ってます。バージョンは

# node -v
v0.10.28

そして、Nodeのパッケージマネージャであるnpmも入ってます

# npm

Usage: npm <command>

where <command> is one of:
    add-user, adduser, apihelp, author, bin, bugs, c, cache,
    completion, config, ddp, dedupe, deprecate, docs, edit,
    explore, faq, find, find-dupes, get, help, help-search,
    home, i, info, init, install, isntall, issues, la, link,
    list, ll, ln, login, ls, outdated, owner, pack, prefix,
    prune, publish, r, rb, rebuild, remove, repo, restart, rm,
    root, run-script, s, se, search, set, show, shrinkwrap,
    star, stars, start, stop, submodule, t, tag, test, tst, un,
    uninstall, unlink, unpublish, unstar, up, update, v,
    version, view, whoami

npm <cmd> -h     quick help on <cmd>
npm -l           display full usage info
npm faq          commonly asked questions
npm help <term>  search for help on <term>
npm help npm     involved overview

Specify configs in the ini-formatted file:
    /home/root/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@1.4.9 /usr/lib/node_modules/npm

expressのインストール

npmが入っているので、npmでexpressをグローバルインストールします。

# npm install -g express

expressコマンドを使いたいのですが、そのままではパスが通っていません。どこにインストールされたのか調べます

# find / -name express
/usr/lib/node_modules/iotkit-agent/node_modules/.bin/express
/usr/lib/node_modules/iotkit-agent/node_modules/express
/usr/lib/node_modules/iotkit-agent/node_modules/express/bin/express

どうやらグローバルインストールされたコマンドは.bin以下にシンボリックリンクが配置されるようなので、ここにパスを通します。ただしYoctoでは".bashrc"とか".bash_profile"ではなく、".profile"を読み込むようなので、ホームディレクトリに".profile"ファイルを作成して、そこにexportでパスを追加します。vimも無いようで、viエディタを使います。

# vi .profile

.profileにパスを追加します

export PATH=/usr/lib/node_modules/iotkit-agent/node_modules/.bin:$PATH

読み込み直します(rebootしてちゃんと読み込まれることを確認した方がいいでしょう)

# source .profile

パスを見てみます

# echo $PATH
/usr/lib/node_modules/iotkit-agent/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

expressでNode.jsサーバを作成

ここまで出来たら、あとはカンタンです。expressでフレームワークを生成して、Nodeを起動するだけです。テンプレートエンジンはejsを使います。

# express -e server

   create : server
   create : server/package.json
   create : server/app.js
   create : server/public
   create : server/public/javascripts
   create : server/public/images
   create : server/public/stylesheets
   create : server/public/stylesheets/style.css
   create : server/routes
   create : server/routes/index.js
   create : server/routes/user.js
   create : server/views
   create : server/views/index.ejs

   install dependencies:
     $ cd server && npm install

   run the app:
     $ node app

# cd server && npm install

# node app.js

ifconfigで調べたEdisonのIPアドレスにポート3000でアクセスしてみましょう。

f:id:tomo_watanabe:20141031152141p:plain

というわけで、カンタンにWebサーバを起動させることができます。