LoginSignup
36
38

More than 5 years have passed since last update.

express-generator を使う

Last updated at Posted at 2014-08-28

express-generator で生成したものには Express 4 の流儀っぽいものが含まれているので一回見てみるとよいかもしれない。

インストール

みんなグローバルにつっこんでるけどローカルですむものはローカルにインストールしようよ。

cd /path/to/project
mkdir generator
cd generator
npm install express-generator

使い捨てる

ひながた生成。

./node_modules/.bin/express ../app

必要なくなったら即パージ。

cd ..
rm -rf generator

アプリ起動

かんたん。

cd app
npm install
npm start

みていくポイント

ポートをいじりたい場合

npm start で起動される ./bin/www で指定されている。これはそれっぽく見えないけど JavaScript で書かれている。

アプリの共通設定を変更したい場合

./app.js が該当する。

うえのほうにアプリ共通で利用する middleware の設定が、なかほどに共通のエラー処理が、したのほうに development 環境や production 環境の設定がある。

実処理

./app.js の app.use('/', routes); というところ。これが実処理を設定しているところになる。中身は ./routes/index.js に express.Router を使って書かれてる。

express.Router は Express 4 からの新機能で、リクエスト URL と実処理をうまいこと分離したもの。説明については以下が詳しい。

36
38
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
38