node.js 如何从命令行创建 Express EJS 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10834849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:48:05  来源:igfitidea点击:

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

node.jsexpress

提问by Hoa

I have tried

我试过了

express -e myproject

express -e myproject

However this does not work as expected

然而,这并没有按预期工作

回答by dmh2000

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

so, do >express -t ejs [path]

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

回答by Marian07

How to install expressfrom the command line using express-generatorand use EJS template engine

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


1) Install express-generator globally("-g") if you don't have it already


1) 如果还没有安装 express-generator("-g")

npm install express-generator -g


2.1) Check available commands


2.1) 检查可用命令

express -h 

Result(in express version: 4.13.4):

结果(快速版本: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) Generate an Express app with chosen preferences

2.2) 生成具有所选首选项的 Express 应用程序

express --ejs --git my_app_with_ejs_and_gitignore

Result:

结果:

   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) Navigate into the app directory and use NPM to install dependencies


3)导航到app目录,使用NPM安装依赖

cd my_app_with_ejs_and_gitignore
npm install


Result:


结果:

+-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| `-- [email protected]
|   +-- [email protected]
|   `-- [email protected]
|     `-- [email protected]
+-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
| `-- [email protected]
+-- [email protected]
+-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| |   +-- [email protected]
| |   `-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
| +-- [email protected]
| `-- [email protected]
`-- [email protected]


4) Start the server


4)启动服务器

DEBUG=my_app_with_ejs_and_gitignore:* npm start

Result:

结果:

[email protected] 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) See the result in the browser
Open a browser and navigate to: http://localhost:3000/


5) 在浏览器中查看结果
打开浏览器并导航到:http://localhost:3000/

The page should contain the following text:
Express
Welcome to Express

该页面应包含以下文字:
Express
Welcome to Express

回答by Jagadesha NH

npm install -g express-generator

and then

进而

express -e project-name

this will create a project with ejs template engine

这将使用 ejs 模板引擎创建一个项目

回答by cmd

Express provides a generator (see instructions below), however for a batteries included generator, you might choose my express-no-stressgenerator.

Express 提供了一个发电机(请参阅下面的说明),但是对于包含电池的发电机,您可以选择我的express-no-stress发电机。

Express-no-stress

表达无压力

Includes ES.next via Babel.js, structured logging with Pino, API validation and interactive documentation via Swagger, environment based config with dotenv, linting with ESLint, and Backpack powered builds.

包括通过 Babel.js 的 ES.next、使用 Pino 的结构化日志记录、通过 Swagger 的 API 验证和交互式文档、使用 dotenv 的基于环境的配置、使用 ESLint 的 linting 和 Backpack 支持的构建。

Install

安装

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

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

Generate Project

生成项目

yo express-no-stress myapp

yo express-no-stress myapp

Run

npm run dev

npm run dev



Or use the Express Generatorthat can be used as follows:

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

Install

安装

npm install express-generator -g

npm install express-generator -g

Generate Project

生成项目

express myapp

express myapp

回答by VedTopkar

  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');
    
  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');
    

EDIT: As dmh2000 pointed out, you can also just do express -t ejs

编辑:正如 dmh2000 所指出的,你也可以这样做 express -t ejs

回答by ack

The option to use depends on the installed version of express (check express -V!)

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

It was changed somewhere around version 3.0.0alpha1.

它在 3.0.0alpha1 版左右发生了变化。

It used to be: express -t ejs, now it is: express -eor express --ejs

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

Proof (from express Git repo):

证明(来自快速 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

Morale: NodeJS modules are moving targets, alwayscheck their docs, especially after updating stuff.

士气:NodeJS 模块是不断变化的目标,请务必检查他们的文档,尤其是在更新内容之后。

回答by Hichem ben chaabene

Make sure you have the express generator installed:

确保安装了 express 生成器:

npm install express-generator -g

then to start a project

然后开始一个项目

express myproject

To display the help screen

显示帮助画面

express --h

I hope it helps

我希望它有帮助

回答by 10ZKhaled

Do the following steps: 1. Install ejs:

执行以下步骤: 1. 安装 ejs:

sudo npm install -g ejs

2. Install node express generator:

2. 安装 node express 生成器:

sudo npm install -g express-generator

3. Reboot the system 4. Create app with express generator:

3. 重启系统 4. 使用 express 生成器创建应用程序:

express --view=ejs myApp

回答by Mehdi Raash

Just start you project with this command

用这个命令开始你的项目

express --view=ejs appName

don't forget to install express-generatorglobally by npm install -g express-generator

不要忘记通过以下方式全局安装express-generatornpm install -g express-generator