使用Express Generator创建Node.js应用程序
在本教程中,我们将讨论Node.js Express生成器工具的概念。Express-generator包是一个实用工具,它提供了一个命令行工具,你可以用它来构建你的项目--即创建模板文件夹结构、文件和代码。
简介
这是一个包,你可以在开发Node.js网络应用程序时使用它来简化你的开发时间。顾名思义,Express生成器是一个工具,我们将使用它来生成我们的应用程序,否则,我们将手动创建。
目标
在本教程结束时,你应该能够生成完整的Node.js应用程序文件夹。
前提条件
- 在你的本地机器上安装Node.js。
- Node.js模板引擎的基本知识,在这个应用程序中我们使用
pug模板引擎。
生成Express应用程序
在这一节中,我们将看看Express生成器包,它是如何安装和用于生成文件夹结构的。
让我们先用Express generator创建一个Express应用程序。
npx express-generator --view=pug expressExample
在上面的命令中,我们指示Express-generator生成一个名为expressExample 的应用程序。然后我们指定我们的应用程序需要使用的视图,在我们的例子中,我们选择pug 。
输出。
create : expressExample/
create : expressExample/public/
create : expressExample/public/javascripts/
create : expressExample/public/images/
create : expressExample/public/stylesheets/
create : expressExample/public/stylesheets/style.css
create : expressExample/routes/
create : expressExample/routes/index.js
create : expressExample/routes/users.js
create : expressExample/views/
create : expressExample/views/error.pug
create : expressExample/views/index.pug
create : expressExample/views/layout.pug
create : expressExample/app.js
create : expressExample/package.json
create : expressExample/bin/
create : expressExample/bin/www
change directory:
$ cd express examples
install dependencies:
$ npm install
run the app:
$ DEBUG=expressexample:* npm start
上面的输出给了你一个项目文件夹结构的概述,而直接的指令是让我们为这个应用程序安装依赖性并运行执行。
$ cd expressExample
$ npm install
这将在几秒钟内安装你的node.js应用程序的依赖项,这取决于你的网速。
完成后,在你的Mac或Linux系统中,运行以下命令来启动你的应用程序。
$ DEBUG=expressexample:* npm start
输出。
> expressexample@0.0.0 start
> node ./bin/www
expressexample:server Listening on port 3000 +0ms
如果你使用的是基于Windows的操作系统,运行以下命令来启动你的应用程序。
> set DEBUG=expressexample:* & npm start
你也可以在Windows PowerShell中运行它,如下。
PS> $env:DEBUG='expressexample:*'; npm start
现在打开浏览器,导航到以下网址。
http://localhost:3000
输出。

我们最终的项目文件夹结构。
expressExample
├── app.js
├── bin
│ └── www
├── package.json
├── package-lock.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.pug
├── index.pug
└── layout.pug
├── node_modules
├── packages you installed on the npm install command
现在我们有了一个结构完整的应用程序,让我们看一下如何使用这些生成的文件来创建一个应用程序的例子。
打开``目录上的index.pug 文件,添加以下内容。
extends layout
block content
h1= title
p This is my first express application generated by the Express Generator package
p To print this view, you require knowledge using Pug
现在,在你的项目目录下打开你的app.js ,并按如下所示进行修改。
...............................................
var createError = require('http-errors');
// we are importing the express package
const express = require('express');
// this line imports the path
const path = require('path');
// these packages will aid in application routing
const homePageRouter = require('./routes/home');
const studentEngineeringRouter = require('./routes/engineering-education');
//these packages are used to parse cookie values
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const app = express();
// the following lines are used to setup view templates for our application
// you can also set views of your choice here.
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// we directing the application of which pages to use
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/home', homePageRouter);
app.use('/engineering-education', studentEngineeringRouter);
// this method catches error Not Found
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
你会注意到,你只是修改了路由和其他任何功能,在Express上生成的应用程序是一个正常的Node.js初始化应用程序。
总结
在本教程中,我们讨论了如何使用Express生成器生成一个Node.js-Express应用程序。我们生成了一个项目结构,并在服务器上启动了我们的应用程序,并显示了一个输出结果。我们还看到了如何修改这个应用程序以满足我们的需求。
正如我们所看到的,express-generator包是一个实用工具,它提供了一个命令行工具,你可以用它来构建你的项目--比如创建模板文件夹结构、文件和代码。