LoginSignup
0
0

ExpressをVirtualBox で構築

Last updated at Posted at 2024-02-05

前提

ホストOS : Windows

仮想環境 : oracle vm virtualbox 7.0.14

OS : CentOS7
CentOS-7-x86_64-Minimal-2009.iso

node.jsインストール

リポジトリ追加(時間がかかる)
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
インストール
sudo yum install -y nodejs
確認
node -v
npm -v

参考

Expressをインストールする

アプリケーション用ディレクトリを作成
mkdir express #アプリケーション名(自由)
cd express
npm init #すべてEnterでもOK
Expressモジュールをインストール
npm install express --save # --save package.jsonにexpressの情報が追加
Expressファイル作成
vi index.js #下記参照

下記公式サンプル内容のファイルを作成
ファイル名はnpm initの設定項目entry pont: (index.js)で指定したファイル名(デフォルトはindex.js)

Express公式サンプル作成(デフォルト index.jsで作成)
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Express公式サンプルURL

参考

Firewall設定

3000番ポートを恒久的に許可
sudo firewall-cmd --add-port=3000/tcp --zone=public --permanent
Firewall再起動
sudo firewall-cmd --reload

実行

Express実行
node index.js
ホストOSのWebブラウザでアクセス
http://(ホスト名):3000/

おまけ

80ポートでアクセス

ポート番号変更 (公式サンプル)
const express = require('express')
const app = express()
- const port = 3000
+ const port = 80

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
Firewall設定(http)
sudo firewall-cmd --add-service=http --zone=public --permanent
sudo firewall-cmd --reload
Express実行
node index.js #実行
ホストOSのWebブラウザでアクセス
http://(ホスト名)/

デーモン化

foreverモジュールのインストール
npm install -g forever

「-g」を付与してグローバル環境にインストール

実行・停止
forever start index.js
forever stop index.js

express-generatorの場合の起動時の指定ファイル

Express実行
node bin/www
0
0
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
0
0