**文書の過去の版を表示しています。**
Visual Studio で Node.js と Express のアプリを作成
Visual Studio 2019のインストール。
- Visual Studio をインストール
- インストーラで[Node.js 開発] ワークロードを選択
伊藤PCのNode.jsをインストール
- Windows Installer (.msi) 64bit版をインストール
Express のアプリを作成
- Visual Studio を開く。
- 新しいプロジェクトを作成。
- [新しい基本の Azure Node.js Express 4 アプリケーション プロジェクトの作成] (JavaScript) を選択します。 表示されたダイアログ ボックスで、 [作成] を選択。
- [デバッグ]を選択しapp.jsが見れることを確認
Apache経由でExpressに接続
httpsの設定ファイルを編集
サーバにhttpdがインストールされていない場合は、以下のコマンドでインストールし、有効化.
$ sudo yum update
$ sudo yum install httpd
$ sudo systemctl enable httpd
$ sudo systemctl start httpd
/etc/httpd/conf.d/の直下に、hoge.confというファイルを作成。(hogeは変更可能)
今回はApacheをプロキシのような中継サーバとして使うので、proxy.confとした。
ProxyPass /metasearch http://localhost:3000/
ProxyPassReverse /metasearch http://localhost:3000/
3000はExpressが使用するデフォルトのポート番号。
httpdを再起動し、設定を反映。
$ sudo systemctl restart httpd
Node.isのインストールとExpressサーバの起動
node.jsをダウンロード、インストール(解凍しただけ)
伊藤PCに入れたnode.jsと同じバージョンをインストール
wget https://nodejs.org/dist/v14.15.3/node-v14.15.3-linux-x64.tar.xz
xz -dc node-v14.15.3-linux-x64.tar.xz | tar xfv -
node、npm、node_modulesにパスを通す。(~/.bashrcに以下3行を追加)
export NODE_HOME=$HOME/node-v14.15.3-linux-x64
export NODEPATH=$NODEHOME/lib/node_modules
export PATH=$NODE_HOME/bin:$PATH
express-generatorをインストール
$ npm install express-generator -g
expressの新しいプロジェクトを作成
$ express exp-app
$ cd exp-app
$ npm install
$ npm start &
以降もターミナルで操作を続けるために、npm start は&を付けてバックグラウンドで実行
httpでアクセスするための設定変更(多分いらない)
FWの設定
$ sudo firewall-cmd –add-service=http –zone=public –permanent
$ sudo firewall-cmd –reload
SElinuxの設定
$ sudo setsebool -P httpdcannetwork_connect on
アクセス確認
Node.jsアプリをLinux環境で常駐化させる
foreverのインストール
$ npm install -g forever
- Expressアプリの起動
reboot時にExpressアプリを自動起動
- bashスクリプトファイルの作成
- 作成したファイルにreboot時に実行するコマンドを書き込む
- bashスクリプトに権限付与
- bashスクリプトをテスト
- crontabを起動
- crontabにreboot時にbashスクリプトファイルを実行するコマンドを書き込む