1.安装express应用生成器
打开cmd 键入如下代码
$npm install express-generator -g #全局安装
安装成功如下-----
2.express命令
打开cmd 键入$express -h 显示所有可用的命令行选项
C:\Users\Administrator>express -h
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|sas
s) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
3.生成应用
在E盘新建一个文件夹test 打开命令窗口 键入 $express first -e 该命令的意思是:在当前目录下建立first文件夹 创建一个ejs模板的应用(如果不输入 -e 则生成默认的jade模板)
E:\test>express first -e
create : first
create : first/package.json
create : first/app.js
create : first/public
create : first/public/javascripts
create : first/public/images
create : first/routes
create : first/routes/index.js
create : first/routes/users.js
create : first/views
create : first/views/index.ejs
create : first/views/error.ejs
create : first/public/stylesheets
create : first/public/stylesheets/style.css
create : first/bin
create : first/bin/www
install dependencies:
> cd first && npm install
run the app:
> SET DEBUG=first:* & npm start
完成后文件夹显示如下
然后键入命令
$cd first #进入first文件夹
$npm install #安装依赖包
安装完成后会发现多了个node_modules文件夹 里面放的就是我们安装的模块
注:
app.js
:启动文件,项目的入口
package.json :存储项目的信息及模块依赖,当在 dependencies 中添加依赖的模块时,运行
node_modules :存放 package.json 中安装的模块,当你在 package.json 添加依赖的模块并安装后,存放在这个文件夹下
public :存放 image、css、js 等文件
routes :存放路由文件
views :存放视图文件或者说模版文件
bin :存放可执行文件
package.json :存储项目的信息及模块依赖,当在 dependencies 中添加依赖的模块时,运行
npm install
,npm 会检查当前目录下的 package.json,并自动安装所有依赖模块
node_modules :存放 package.json 中安装的模块,当你在 package.json 添加依赖的模块并安装后,存放在这个文件夹下
public :存放 image、css、js 等文件
routes :存放路由文件
views :存放视图文件或者说模版文件
bin :存放可执行文件
4.启动应用
键入命令$npm start 启动成功如下:
E:\test\first>npm start
> first@0.0.0 start E:\test\first
> node ./bin/www
5.访问应用
然后在浏览器中打开
http:
//localhost:3000/ 网址你可以看到welcome to Express,证明你已经成功了
其他
express官网
http://www.expressjs.com.cn/