ExpressJS - 腳手架



腳手架允許我們輕鬆建立Web應用程式的框架。我們手動建立公共目錄,新增中介軟體,建立單獨的路由檔案等。腳手架工具為我們設定所有這些內容,以便我們可以直接開始構建應用程式。

我們將使用的腳手架工具稱為Yeoman。它是一個為Node.js構建的腳手架工具,但也有幾個其他框架的生成器(如Flask、Rails、Django等)。要安裝Yeoman,請在您的終端中輸入以下命令:

npm install -g yeoman

Yeoman使用生成器來構建應用程式。要檢視可在npm上使用的Yeoman生成器,您可以點選此連結。在本教程中,我們將使用'generator-Express-simple'。要安裝此生成器,請在您的終端中輸入以下命令:

npm install -g generator-express-simple

要使用此生成器,請輸入以下命令:

yo express-simple test-app

系統會詢問您一些簡單的問題,例如您想在應用程式中使用哪些內容。選擇以下答案,或者如果您已經瞭解這些技術,那麼您可以選擇自己想要的方式。

express-simple comes with bootstrap and jquery
[?] Select the express version you want: 4.x
[?] Do you want an mvc express app: Yes
[?] Select the css preprocessor you would like to use: sass
[?] Select view engine you would like to use: jade
[?] Select the build tool you want to use for this project: gulp
[?] Select the build tool you want to use for this project: gulp
[?] Select the language you want to use for the build tool: javascript
   create public/sass/styles.scss
   create public/js/main.js
   create views/layout.jade
   create views/index.jade
   create views/404.jade
   create app.js
   create config.js
   create routes/index.js
   create package.json
   create bower.json
identical .bowerrc
identical .editorconfig
identical .gitignore
identical .jshintrc
   create gulpfile.js

I'm all done. Running bower install & npm install for you to install the
required dependencies. If this fails, try running the command yourself.

然後,它將為您建立一個新的應用程式,安裝所有依賴項,向您的應用程式新增一些頁面(主頁、404 未找到頁面等),併為您提供一個目錄結構來進行操作。

此生成器為我們建立了一個非常簡單的結構。探索Express提供的許多生成器,並選擇最適合您的一個。所有生成器的使用方法都相同。您需要安裝生成器,使用Yeoman執行它;它會問您一些問題,然後根據您的回答建立應用程式的框架。

廣告