LoginSignup
5
4

More than 5 years have passed since last update.

Express.jsはexpress-generatorを入れてnpm startで実行

Posted at

はじめに

技術評論社「基礎から学ぶNode.js」がとてもわかりやすく重宝している。ただ、執筆時点(2012年)から5年経っておりチュートリアル通りに動作しなかった箇所があったので、修正した手順を記録する。

「第3回 Express.jsを使ったWebアプリケーションを構築」でハマった

第3回の記事はExpressフレームワークを使ったWebアプリのチュートリアルなのだが、最新のExpress.jsでは以下のように手順を置き換える必要がある。

まず、expressだけでなくexpress-generatorもインストールすること。

npm install -g express
npm install -g express-generator
express --version
4.15.0

このexpressコマンドはexpress-generatorを実行するプログラムである(ややこしい)。次にEJS(Embedded JavaScript)をテンプレートとしてfirstappアプリを作成し、npm install -dでモジュールをインストールする。ここは元記事の通りでOK。

express -t ejs firstapp
cd firstapp
npm install -d

そしてアプリを実行する訳だが、元記事のように

node app.js

と打っても何も起きない( ;∀;) そうではなく、

npm start

と打てば、無事にアプリが実行される。ブラウザーから http://127.0.0.1:3000/ にアクセスすれば動作確認が可能。

以上

5
4
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
5
4