如何通过命令行创建一个Express EJS项目?

8

我尝试过

express -e myproject

但是效果不如预期


4
“Does not work as expected”? 它实际上做了什么? - Pero P.
10个回答

18
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] 命令


8
自 Express 4.x 版本开始,安装 express-generator 命令行工具 npm install -g express-generator ,然后使用 express -e myproject 创建项目。其中,-e 参数表示使用 EJS 模板引擎。 - Jon Saw

10

如何使用express-generator在命令行中安装 express并使用 EJS模板引擎


1)如果您还没有安装,请全局安装(“-g”)express-generator

npm install express-generator -g


2.1) 检查可用命令

express -h 

结果(在 Express 版本 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) 进入应用程序目录并使用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/

页面应该包含以下文本:
Express
欢迎使用 Express


最新版本的Express Generator已将“express --ejs --git my_app_with_ejs_and_gitignore”更改为“express --view=ejs”,需要在相关文件夹上执行此操作。 - Achintha Isuru

4
对我有效的方法是:
npx express-generator --view=ejs

无需预先安装任何东西(当然,除了nodeJS)


3
npm install -g express-generator

然后

express -e project-name

这将创建一个带有ejs模板引擎的项目。


3

只需使用该命令启动您的项目

express --view=ejs appName

不要忘记通过npm install -g express-generator全局安装express-generator


2

使用的选项取决于安装的express版本(检查express -V!)

它在大约3.0.0alpha1版本左右进行了更改。

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

证明(来自express 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模块是移动目标,一定要检查它们的文档,尤其是在更新后。


2

Express提供了一个生成器(请参见下面的说明),但如果您想要一个“电池包含”的生成器,可以选择使用我的express-no-stress生成器。

Express-no-stress

通过Babel.js包含ES.next,结构化日志记录Pino,通过Swagger进行API验证和交互式文档,dotenv进行基于环境的配置,使用ESLint进行代码检查,并通过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


你可能想阅读《如何提供个人开源库?》(//meta.stackexchange.com/q/229085)。 - Martijn Pieters

2

请按照以下步骤操作: 1. 安装ejs:

sudo npm install -g ejs

2. 安装 Node Express Generator:

sudo npm install -g express-generator

3. 重新启动系统 4. 使用express generator创建应用:

express --view=ejs myApp

2
  1. Install express globally npm install -g express
  2. In terminal, go to the directory in which you want your project to reside. If you are in the directory that you want the files to be in, just type express. If you want express to make a subfolder for the project, type express appname.
  3. To install EJS, use npm install ejs
  4. To configure EJS in your express project, you have to make sure you have the following line in your app.config function:

    app.set('view engine', 'ejs');
    
编辑:正如dmh2000指出的那样,您也可以只执行express -t ejs

1

请确保您已安装express generator:

npm install express-generator -g

然后开始一个项目。
express myproject

显示帮助屏幕。
express --h

我希望它有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接