express创建ejs模块

准备工作

1、安装cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

淘宝npm镜像链接

1、安装express环境

cnpm install express-generator -g

可通过以下代码查看所有可用的命令行参数

express -h

2、创建名称为 stuadmin 的 Express 应用。此应用将在当前目录下的stuadmin 目录中创建,并且设置为使用 ejs 模板引擎

express --view=ejs stuadmin

结果如下:

D:\>"node"  "C:\Users\Direct\AppData\Roaming\npm\\node_modules\express-generator
\bin\express-cli.js" --view=ejs stuadmin

   create : stuadmin\
   create : stuadmin\public\
   create : stuadmin\public\javascripts\
   create : stuadmin\public\images\
   create : stuadmin\public\stylesheets\
   create : stuadmin\public\stylesheets\style.css
   create : stuadmin\routes\
   create : stuadmin\routes\index.js
   create : stuadmin\routes\users.js
   create : stuadmin\views\
   create : stuadmin\views\error.ejs
   create : stuadmin\views\index.ejs
   create : stuadmin\app.js
   create : stuadmin\package.json
   create : stuadmin\bin\
   create : stuadmin\bin\www

   change directory:
     > cd stuadmin

   install dependencies:
     > npm install

   run the app:
     > SET DEBUG=stuadmin:* & npm start

进入stuadmin文件夹,并安装所有组件

cd stuadmin
cnpm install

结果如下:

D:\>cd stuadmin

D:\stuadmin>cnpm install
√ Installed 6 packages
√ Linked 47 latest versions
√ Run 0 scripts
Recently updated (since 2019-11-07): 1 packages (detail see file D:\stuadmin\nod
e_modules\.recently_updates.txt)
√ All packages installed (54 packages installed from npm registry, used 1s(netw
ork 1s), speed 122.87kB/s, json 53(125.82kB), tarball 0B)

D:\stuadmin>


3、Express使用nodemon实现热更新

cnpm install --save-dev nodemon

结果如下:

D:\stuadmin>cnpm install --save-dev nodemon
/ [0/1] Installing is-buffer@^1.1.5platform unsupported [email protected] › chokida
[email protected] › fsevents@^1.2.7 Package require os(darwin) not compatible with your pl
atform(win32)
[fsevents@^1.2.7] optional install error: Package require os(darwin) not compati
ble with your platform(win32)
√ Installed 1 packages
√ Linked 176 latest versions
[1/1] scripts.postinstall nodemon@* run "node bin/postinstall || exit 0", root:
"D:\\stuadmin\\node_modules\\[email protected]@nodemon"
Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

[1/1] scripts.postinstall nodemon@* finished in 345ms
√ Run 1 scripts
deprecate [email protected][email protected] › fsevents@^1.2.7 One of your dependen
cies needs to upgrade to fsevents v2: 1) Proper nodejs v10+ support 2) No more f
etching binaries from AWS, smaller package size
√ All packages installed (192 packages installed from npm registry, used 5s(net
work 4s), speed 60.41kB/s, json 177(255.13kB), tarball 0B)

D:\stuadmin>

2、 修改配置文件,在package.json中的scripts中配置运行项即可

原来配置文件如下:

"scripts": {
    "start": "node ./bin/www"
  },

更改后如下:

 "scripts": {
    "start": "nodemon ./bin/www"
  },

express就此安装完成!!!!
运行以下代码即可运行express应用并实现热更新

cnpm start

结果如下:

D:\stuadmin>cnpm start

> [email protected] start D:\stuadmin
> nodemon ./bin/www


D:\stuadmin>"node"  "D:\stuadmin\node_modules\.bin\\..\[email protected]@nodemon\b
in\nodemon.js" ./bin/www
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`

打开浏览器输入以下地址即可进入对应的初始页面
1、http://localhost:3000/
http://localhost:3000/
2、http://localhost:3000/users
在这里插入图片描述

自所有工作调试完成!!!
纯属自用记录,如有侵权请留言博主及时修改或删除,谢谢!

发布了18 篇原创文章 · 获赞 4 · 访问量 556

猜你喜欢

转载自blog.csdn.net/weixin_45538576/article/details/103062943