0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Expressでプロジェクトディレクトリを構築する方法

Posted at

Expressでプロジェクトディレクトリを構築する方法

ExpressはNode.jsでWebアプリやAPIを構築するためのフレームワークです。本記事では、Expressのプロジェクトディレクトリ構築の基本的な流れを解説します。


経済環境の準備

Expressを使用する前に、次の準備を行います。

1. Node.jsとnpmのインストール

最新版のNode.js をインストールします。

インストール完了後、次のコマンドでバージョンを確認します。

node -v
npm -v

Expressのプロジェクト構築

1. express-generatorのインストール

Expressのテンプレートを構築するために、express-generatorをグローバルインストールします。

npm install -g express-generator

2. プロジェクトを新規作成

プロジェクト名を指定する場合

下記のコマンドで新しいプロジェクトディレクトリを構築します。

express my-express-app

現在のディレクトリに展開する場合

現在のディレクトリにプロジェクトを構築したい場合、以下のように実行します。

express .

3. 依存パッケージのインストール

プロジェクトディレクトリに移動し、次のコマンドを実行します。

npm install

4. サーバーの起動

次のコマンドを実行して、Expressサーバーを起動します。

npm start

ブラウザでhttp://localhost:3000にアクセスすると、初期ページが表示されます。


Expressのプロジェクト構造

express-generatorを使用した場合のデフォルト構造は次の通りです。

my-express-app/
├── app.js              # アプリケーションのエントリポイント
├── package.json        # 依存関係とスクリプト
├── bin/
│   └── www             # サーバー起動スクリプト
├── public/             # 非動ファイル (CSS, JavaScript, 画像)
├── routes/             # ルート定義
│   ├── index.js
│   └── users.js
├── views/              # テンプレートファイル (Pug, EJSなど)
│   ├── error.pug
│   ├── index.pug
│   └── layout.pug

カスタマイズ

  • ルーティングの追加: routesディレクトリに新規ファイルを作成して追加。
  • テンプレートエンジンの変更: 必要に応じてPug以外のテンプレートエンジン (EJS, Handlebars) をインストール。
  • 非動ファイルの配置: public/ディレクトリに画像やCSSファイルを配置。
  • ミドルウェアの追加: 必要なミドルウェアをapp.jsに追加。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?