Sails.jsで提供されるコマンドについての説明になります。
プロジェクト
新規作成 > new
まずはプロジェクトの立ち上げから。
このコマンドが、アプリ製作の第一歩です!
rascal@RascalRoom:~# sails new { プロジェクト名 }
例: プロジェクト名を「app1」とした場合、
rascal@RascalRoom:~# sails new app1 Choose a template for your new Sails app: 1. Web App ・ Extensible project with auth, login, & password recovery 2. Empty ・ An empty Sails app, yours to configure (type "?" for help, or <CTRL+C> to cancel) ? 2 info: Installing dependencies... Press CTRL+C to cancel. (to skip this step in the future, use --fast) info: Created a new Sails app `app-1`!
newを実行すると、「新しいSailsアプリのテンプレートを選択してください」みたいなのを聞いてくるので、
認証やログイン、パスワード回復等が実装済みのものを使う場合は「1」。
デフォルトのテンプレートを使う場合は「2」を選択します。
ユーザーごとの認証情報を扱う時は、こだわりがなければ「1」 で作ると、サクッとできて簡単です。
基本的には「2」でいいかなと。
また、
rascal@RascalRoom:~# sails new app1 --fast
とすると、npmモジュールが空の状態でプロジェクトを作成します。
下記を実行すると最低限のモジュールだけ使うことができます。
rascal@RascalRoom:~# cd app1 rascal@RascalRoom:~/app1# npm install
--fast をつけることで、通常より早く作成が完了します。
--no-frontend をつけると、「Gruntfile.js」「assets」「scripts」「tasks」「views」などがない状態で作成されます。
フロントエンドを使用しない場合に使うらしいですが、僕はその状況に出会ったことがないので、理解できてません。。。
実行 > lift
プロジェクトの実行の仕方です。
rascal@RascalRoom:~/app1# sails lift info: Starting app... info: Initializing project hook... (`api/hooks/custom/`) info: Initializing `apianalytics` hook... (requests to monitored routes will be logged!) info: ・? Auto-migrating... (alter) info: Hold tight, this could take a moment. info: ? Auto-migration complete. debug: Skipping v0 bootstrap script... (because it's already been run) debug: (last run on this computer: @ Sat Jun 29 2019 14:56:45 GMT+0000 (Coordinated Universal Time)) info: info: .-..-. info: info: Sails <| .-..-. info: v1.2.3 | info: /|. info: / || info: ,' |' info: .-'.-==|/_--' info: `--'-------' info: __---___--___---___--___---___--___ info: ____---___--___---___--___---___--___-__ info: info: Server lifted in `/app/app1` info: To shut down Sails, press+ C at any time. info: Read more at https://sailsjs.com/support. debug: ------------------------------------------------------- debug: :: Sat Jun 29 2019 15:03:31 GMT+0000 (Coordinated Universal Time) debug: Environment : development debug: Port : 1337 debug: -------------------------------------------------------
これで実行が完了しました。
早速、ブラウザからつないでみましょう!
ポート番号は「1337」でリッスンするので、
URLは「http://{ Your IP Address or FQDN }:1337」となります。
new した後のデフォルトでは、上のような画面が出てきます。
初回の立ち上げの際は、接続に問題ないかテストしてもいいでしょうね。
ポート番号は任意に変更ができます。
その際は、--port オプションを使います。
rascal@RascalRoom:~/app1# sails lift --port { ポート番号 }
また、
プロジェクト作成時に、「認証やログイン、パスワード回復等が実装済み」とした場合のデフォルト画面はこうなります。
(テンプレート選択に「1」を指定したとき)
右上の「Sign up」をクリックすると、
アカウント作成ページが表示されます。
「Log in」をクリックすれば、
ログイン画面に移ります。
こういうユーザー情報管理って自分で作ろうとするとめんどうなので、便利で嬉しい機能ですね。
API
Sails.jsはMVCを採用しているので、モデル(M)とコントローラー(C)を実装していきます。
generate
モデル > model
Modelなので、データベースとやり取りをするORMの定義と思ってください。
MySQLなどのRDBなら "テーブル" 、MongoDBなら "ドキュメント" のスキーマを定義します。
MongoDBについてはこちらの記事が参考になります
では、モデルの作り方です。
rascal@RascalRoom:~/app1# sails generate model { モデル名 }
例: 「model1」モデルを作成する場合
rascal@RascalRoom:~/app1# sails generate model model1 info: Created a new model ("Model1")!
/** * Model1.js * * @description :: A model definition represents a database table/collection. * @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models */ module.exports = { attributes: { // ???????????????? ??????? // ??????????? ? ??????? ??? // ? ????? ?? ? ? ?? ?????? // ???????? ????????? // ?? ???????? ????? // ???? ????????????? // ???????????????????????????????? // ?????????? ?? ???? ? ?? ??????? // ? ??????????????? ? ? ?????????? }, };
(文字化けしてるわ。。。)
Railsにはモデルやコントローラーの名前に命名規則があるかと思いますが、Sailsには特にないと思ってもらって構いません。
単数形、複数形みたいなところは意識したことはないですね。
また、モデル名の後にあらかじめ属性を引数にしておくこともできます。
rascal@RascalRoom:~/app1# sails generate model model2 attr1 attr2 info: Created a new model ("Model2")!
/** * Model2.js * * @description :: A model definition represents a database table/collection. * @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models */ module.exports = { attributes: { // ???????????????? ??????? // ??????????? ? ??????? ??? // ? ????? ?? ? ? ?? ?????? attr1: { type: 'string' }, attr2: { type: 'string' } // ???????? ????????? // ?? ???????? ????? // ???? ????????????? // ???????????????????????????????? // ?????????? ?? ???? ? ?? ??????? // ? ??????????????? ? ? ?????????? }, };
string型で登録されましたね。
コントローラー > contoller
コントローラーの作り方です。
rascal@RascalRoom:~/app1# sails generate controller { コントローラー名 }
例: 「controller1」コントローラーを作成する場合
rascal@RascalRoom:~/app1# sails generate controller controller1 info: Created a new controller ("controller1") at api/controllers/Controller1Controller.js!
/** * Controller1Controller * * @description :: Server-side actions for handling incoming requests. * @help :: See https://sailsjs.com/docs/concepts/actions */ module.exports = { };
アクションをあらかじめ定義する場合はこうなります。
rascal@RascalRoom:~/app1# sails generate controller controller2 action1 action2 info: Created a new controller ("controller2") at api/controllers/Controller2Controller.js!
/** * Controller2Controller * * @description :: Server-side actions for handling incoming requests. * @help :: See https://sailsjs.com/docs/concepts/actions */ module.exports = { /** * `Controller2Controller.action1()` */ action1: async function (req, res) { return res.json({ todo: 'action1() is not implemented yet!' }); }, /** * `Controller2Controller.action2()` */ action2: async function (req, res) { return res.json({ todo: 'action2() is not implemented yet!' }); } };
モデルとコントローラーをまとめて
rascal@RascalRoom:~/app1# sails generate api { api名 }
こうするとモデルとコントローラーがまとめて作成されます。
モデル「api/models/Foo.js」、コントローラー「api/controllers/FooController.js」となります。(api名を Foo とした場合)
その他の参考
Sails.jsの公式サイトにいろいろ載っているので、興味のある方はどうぞ。
まとめ
よく使うコマンドはこれくらいですね。
MVCについては、紹介している投稿をよく見かけるので、wikiでも載せておきます。
次回は、ディレクトリの構造と基本的なところの説明をしていきます。