指定している# API 作成のために Express を改めてセッティングする

Express-generator のインストールと雛形の作成

Express アプリケーションの雛形を作るための CLI をインストールする。

$ yarn global add express-generator

プロジェクトディレクトリ直下で、以下のコマンドを実行して Express アプリケーションの雛形を作成する。

$ express

パッケージをインストールする

$ yarn

ここまで Webstorm ユーザーであれば、express-generator を Global にインストールした後に、New Project から Express で、自動的に行なってくれる。

express による server を立ち上げてクライアントで接続テスト

$ yarn start

もしくは

$ node ./bin/www

を実行して express による server を立ち上げて、ブラウザから localhost:3000 にアクセスする。(./bin/www に実行ファイルがある。)

成功していれば welcome to express とブラウザに表示される。

jadeを使わないので其の部分を削除して、かわりにget()で読み込ませるものを与える

/app.js の view に jade を使うことを指定している、以下の箇所をコメントアウトする。

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

25行目あたりにある以下を削除して、

app.use('/', index);
app.use('/users', users);

同じ位置に次のものを配置する

app.get('*', function(req, res){
res.sendFile(path.resolve(__dirname,
'public', 'index.html'))
})

app.get で読み込むファイル /public/index.js を作成する

yarn start を再起動して、localhost:3000 にアクセスして、先程作った index.html が表示されれば OK

mongoDB のためのセッティング

$ yarn add mongoose  
$ yarn global add nodemon \(一度も使ったことがなければ Global にも\)  
$ yarn add nodemon

Restart the server using the command

$ nodemon

results matching ""

    No results matching ""