NodeJS express 项目环境搭建/目录结构生成 express-generator
Intro
两种方式,node自己就有模块用于自动生成使用express框架的开发环境,叫expresss-generator
,
另外,Debian系的Linux发行版上也有一个node-express-generator
工具,安装即可使用。
NodeJS express-generator
命令
npm ll -g express express-generator
先查询全局是否已经安装npm install -g aaa@qq.com aaa@qq.com
安装这两个模块(使用@可以指定版本)npm ll -g express express-generator
再次查询安装情况
which express
查询在命令行直接调用express
命令,是哪个位置的命令。ll
which express`` 查看该程序文件的具体信息(如大小、文件链接源)
可以看到,这实际上是express-generator
替我们安装的一个命令工具(见执行过程)。express --help
查看帮助信息
express /tmp/foo && cd /tmp/foo
使用express-generator
中的express
/tmp/foo
查看生成目录的结构层次
如果想卸载这两个模块:npm uninstall -g express express-generator
执行过程
aaa@qq.com:~$ npm ll -g express express-generator
│ /opt/node/lib
│
└── (empty)
aaa@qq.com:~$ npm install -g aaa@qq.com aaa@qq.com
npm WARN deprecated aaa@qq.com: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
/opt/node/bin/express -> /opt/node/lib/node_modules/express-generator/bin/express-cli.js
+ aaa@qq.com
+ aaa@qq.com
added 60 packages from 42 contributors in 3.309s
aaa@qq.com:~$ npm ll -g express express-generator
│ /opt/node/lib
│
├── aaa@qq.com
│ Fast, unopinionated, minimalist web framework
│ git+https://github.com/expressjs/express.git
│ http://expressjs.com/
└── aaa@qq.com
Express' application generator
git+https://github.com/expressjs/generator.git
https://github.com/expressjs/generator#readme
aaa@qq.com:~$
aaa@qq.com:~$ which express
/opt/node/bin/express
aaa@qq.com:~$ ll `which express`
lrwxrwxrwx 1 wuyujin wuyujin 56 4月 6 21:23 /opt/node/bin/express -> ../lib/node_modules/express-generator/bin/express-cli.js*
aaa@qq.com:~$
aaa@qq.com:~$ express --help
Usage: express [options] [dir]
Options:
--version output the version number
-e, --ejs add ejs engine support
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-v, --view <engine> add view <engine> support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
--no-view use static html instead of view engine
-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
-h, --help output usage information
aaa@qq.com:~$
aaa@qq.com:~$ express /tmp/foo && cd /tmp/foo
warning: the default view engine will not be jade in future releases
warning: use `--view=jade' or `--help' for additional options
create : /tmp/foo/
create : /tmp/foo/public/
create : /tmp/foo/public/javascripts/
create : /tmp/foo/public/images/
create : /tmp/foo/public/stylesheets/
create : /tmp/foo/public/stylesheets/style.css
create : /tmp/foo/routes/
create : /tmp/foo/routes/index.js
create : /tmp/foo/routes/users.js
create : /tmp/foo/views/
create : /tmp/foo/views/error.jade
create : /tmp/foo/views/index.jade
create : /tmp/foo/views/layout.jade
create : /tmp/foo/app.js
create : /tmp/foo/package.json
create : /tmp/foo/bin/
create : /tmp/foo/bin/www
change directory:
$ cd /tmp/foo
install dependencies:
$ npm install
run the app:
$ DEBUG=foo:* npm start
aaa@qq.com:/tmp/foo$ tree /tmp/foo
/tmp/foo
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
7 directories, 9 files
aaa@qq.com:/tmp/foo$
Linux node-express-generator
如果你使用的是Debian系的Linux发行版,也可以在系统的层面装一个工具node-express-generator
。
命令
sudo apt-get install node-express-generator
dpkg -L node-express-generator | grep bin
定位安装好的可执行文件的路径(一般在bin目录中)which express
whatis express
express --help
express /tmp/foo
tree /tmp/foo
查看生成的目标目录的层次结构
如果想要卸载系统层面的node-express-generator
工具:sudo apt-get remove --purge node-express-generator
卸载并清除(purge
)配置文件
执行过程
aaa@qq.com:~$ dpkg -L node-express-generator | grep bin
/usr/lib/nodejs/express-generator/bin
/usr/lib/nodejs/express-generator/bin/express
/usr/bin
/usr/bin/express
aaa@qq.com:~$ which express
/usr/bin/express
aaa@qq.com:~$ whatis express
express (1) - the quickest way to start using express framework
aaa@qq.com:~$ express --help
Usage: express [options] [dir]
Options:
-V, --version output the version number
-e, --ejs add ejs engine support (defaults to jade)
-H, --hogan add hogan.js engine support
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass) (defaults to plain css)
-f, --force force on non-empty directory
-h, --help output usage information
aaa@qq.com:~$ express /tmp/foo
create : /tmp/foo
create : /tmp/foo/package.json
create : /tmp/foo/app.js
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
create : /tmp/foo/public
create : /tmp/foo/public/stylesheets
create : /tmp/foo/public/stylesheets/style.css
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
create : /tmp/foo/routes
create : /tmp/foo/routes/index.js
create : /tmp/foo/routes/users.js
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
create : /tmp/foo/views
create : /tmp/foo/views/index.jade
create : /tmp/foo/views/layout.jade
create : /tmp/foo/views/error.jade
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
create : /tmp/foo/bin
create : /tmp/foo/bin/www
(node:11737) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
create : /tmp/foo/public/javascripts
create : /tmp/foo/public/images
install dependencies:
$ cd /tmp/foo && npm install
run the app:
$ DEBUG=my-application ./bin/www
aaa@qq.com:~$ tree /tmp/foo
/tmp/foo
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
7 directories, 9 files
aaa@qq.com:~$
运行和访问
npm install
安装模块npm start
启动服务
然后访问localhost:3000即可。3000是默认的端口。curl localhost:3000
Recommend
NodeJS + express + express-generator
毕竟二者都是Node的模块,管理、学习和使用起来都会更方便。