簡體   English   中英

如何從命令行創建 Express EJS 項目?

[英]How can I create an Express EJS project from the command line?

我努力了

express -e myproject

然而,這並沒有按預期工作

express --help

  Usage: express [options] [path]

  Options:
    -s, --sessions           add session support
    -t, --template <engine>  add template <engine> support (jade|ejs). default=jade
    -c, --css <engine>       add stylesheet <engine> support (stylus). default=plain css
    -v, --version            output framework version
    -h, --help               output help information

所以,做 >express -t ejs [path]

如何使用express-generator從命令行安裝express並使用EJS 模板引擎


1) 如果還沒有安裝 express-generator("-g")

npm install express-generator -g


2.1) 檢查可用命令

express -h 

結果(快速版本:4.13.4):

  Usage: express [options] [dir]

  Options:
-h, --help          output usage information
-V, --version       output the version number
-e, --ejs           add ejs engine support (defaults to jade)
    --hbs           add handlebars engine support
-H, --hogan         add hogan.js engine support
-c, --css <engine>  add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
    --git           add .gitignore
-f, --force         force on non-empty directory

2.2) 生成具有所選首選項的 Express 應用程序

express --ejs --git my_app_with_ejs_and_gitignore

結果:

   create : my_app_with_ejs_and_gitignore
   create : my_app_with_ejs_and_gitignore/package.json
   create : my_app_with_ejs_and_gitignore/app.js
   create : my_app_with_ejs_and_gitignore/.gitignore
   create : my_app_with_ejs_and_gitignore/public
   create : my_app_with_ejs_and_gitignore/public/javascripts
   create : my_app_with_ejs_and_gitignore/public/images
   create : my_app_with_ejs_and_gitignore/public/stylesheets
   create : my_app_with_ejs_and_gitignore/public/stylesheets/style.css
   create : my_app_with_ejs_and_gitignore/routes
   create : my_app_with_ejs_and_gitignore/routes/index.js
   create : my_app_with_ejs_and_gitignore/routes/users.js
   create : my_app_with_ejs_and_gitignore/views
   create : my_app_with_ejs_and_gitignore/views/index.ejs
   create : my_app_with_ejs_and_gitignore/views/error.ejs
   create : my_app_with_ejs_and_gitignore/bin
   create : my_app_with_ejs_and_gitignore/bin/www

   install dependencies:
     $ cd my_app_with_ejs_and_gitignore && npm install

   run the app:
     $ DEBUG=my_app_with_ejs_and_gitignore:* npm start


3)導航到app目錄,使用NPM安裝依賴

cd my_app_with_ejs_and_gitignore
npm install


結果:

+-- body-parser@1.15.2
| +-- bytes@2.4.0
| +-- content-type@1.0.2
| +-- depd@1.1.0
| +-- http-errors@1.5.0
| | +-- inherits@2.0.1
| | +-- setprototypeof@1.0.1
| | `-- statuses@1.3.0
| +-- iconv-lite@0.4.13
| +-- on-finished@2.3.0
| | `-- ee-first@1.1.1
| +-- qs@6.2.0
| +-- raw-body@2.1.7
| | `-- unpipe@1.0.0
| `-- type-is@1.6.13
|   +-- media-typer@0.3.0
|   `-- mime-types@2.1.11
|     `-- mime-db@1.23.0
+-- cookie-parser@1.4.3
| +-- cookie@0.3.1
| `-- cookie-signature@1.0.6
+-- debug@2.2.0
| `-- ms@0.7.1
+-- ejs@2.4.2
+-- express@4.13.4
| +-- accepts@1.2.13
| | `-- negotiator@0.5.3
| +-- array-flatten@1.1.1
| +-- content-disposition@0.5.1
| +-- cookie@0.1.5
| +-- escape-html@1.0.3
| +-- etag@1.7.0
| +-- finalhandler@0.4.1
| +-- fresh@0.3.0
| +-- merge-descriptors@1.0.1
| +-- methods@1.1.2
| +-- parseurl@1.3.1
| +-- path-to-regexp@0.1.7
| +-- proxy-addr@1.0.10
| | +-- forwarded@0.1.0
| | `-- ipaddr.js@1.0.5
| +-- qs@4.0.0
| +-- range-parser@1.0.3
| +-- send@0.13.1
| | +-- destroy@1.0.4
| | +-- http-errors@1.3.1
| | +-- mime@1.3.4
| | `-- statuses@1.2.1
| +-- serve-static@1.10.3
| | `-- send@0.13.2
| |   +-- http-errors@1.3.1
| |   `-- statuses@1.2.1
| +-- utils-merge@1.0.0
| `-- vary@1.0.1
+-- morgan@1.7.0
| +-- basic-auth@1.0.4
| `-- on-headers@1.0.1
`-- serve-favicon@2.3.0


4)啟動服務器

DEBUG=my_app_with_ejs_and_gitignore:* npm start

結果:

my_app_with_ejs_and_gitignore@0.0.0 start C:\Users\Marian\OneDrive\Documente\Practice\Node\express_generator_2\my_app_with_ejs_and_gitignore
node ./bin/www
Sun, 31 Jul 2016 13:51:25 GMT my_app_with_ejs_and_gitignore:server Listening on port 3000


5) 在瀏覽器中查看結果
打開瀏覽器並導航到: http://localhost:3000/

該頁面應包含以下文本:
特快列車
歡迎使用快遞

npm install -g express-generator

接着

express -e project-name

這將使用 ejs 模板引擎創建一個項目

用這個命令開始你的項目

express --view=ejs appName

不要忘了通過全球范圍內安裝快遞發電機npm install -g express-generator

  1. 全局安裝 express npm install -g express
  2. 在終端中,轉到您希望項目所在的目錄。 如果您位於希望文件所在的目錄中,只需鍵入express 如果要 express 為項目創建子文件夾,請鍵入express appname
  3. 要安裝 EJS,請使用npm install ejs
  4. 要在你的 express 項目中配置 EJS,你必須確保你的 app.config 函數中有以下行:

     app.set('view engine', 'ejs');

編輯:正如 dmh2000 指出的,你也可以只做express -t ejs

使用的選項取決於安裝的 express 版本(檢查express -V !)

它在 3.0.0alpha1 版左右發生了變化。

以前是: express -t ejs ,現在是: express -eexpress --ejs

證明(來自快速 Git 存儲庫):

git log -S'--ejs' # Search for the change using pickaxe
git show 29508f1 # The commit
git cat-file blob 29508f1:package.json|grep version # express version

士氣:NodeJS 模塊是不斷變化的目標,請務必檢查他們的文檔,尤其是在更新內容之后。

Express 提供了一個發電機(請參閱下面的說明),但是對於包含電池的發電機,您可以選擇我的express-no-stress發電機。

表達無壓力

包括通過 Babel.js 的 ES.next、使用 Pino 的結構化日志記錄、通過 Swagger 的 API 驗證和交互式文檔、使用 dotenv 的基於環境的配置、使用 ESLint 的 linting 和 Backpack 支持的構建。

安裝

npm install -g yo generator-express-no-stress

生成項目

yo express-no-stress myapp

跑步

npm run dev


或者使用可以如下使用的Express Generator

安裝

npm install express-generator -g

生成項目

express myapp

確保安裝了 express 生成器:

npm install express-generator -g

然后開始一個項目

express myproject

顯示幫助畫面

express --h

我希望它有幫助

執行以下步驟: 1. 安裝 ejs:

sudo npm install -g ejs

2. 安裝 node express 生成器:

sudo npm install -g express-generator

3. 重啟系統 4. 使用 express 生成器創建應用程序:

express --view=ejs myApp

對我有用的是

npx express-generator --view=ejs

無需預先安裝任何東西(當然 nodeJS 除外)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM