关于node项目中view里面ejs或者jade后缀名更改成HTML


   看着.ejs或者jade的后缀总觉得不爽,使用如下方法,可以将模板文件的后缀换成我们习惯的.html。

 (1) 修改 app.js 文件,及修改引擎设置文件:

  • 在app.js的头上 添加定义ejs,代码如下:
    var ejs = require('ejs');
  • 添加注册 html 模板引擎,代码如下(两种方式选其一即可):
  • app.engine('html',ejs.__express);    或者   app.engine('html', ejs.renderFile); 

    # 也可以去掉第一步,直接
    app.engine('html', require('ejs').renderFile);
  • 将模板引擎换成html,代码如下:
    app.set('view engine', 'html');

   得到的 app.js 文件如下(整个只更改了红框的三句):

        

 

(2)设置具体的 运行文件 .ejs 后缀全部更改为 .html 后缀。

  • 修改所有模板文件(views 文件夹里的 .ejs 后缀文件)的后缀改为 .html。结果如下红框部分:

           


如果出现require('ejs')报错,即node_module里面没有安装ejs模块,可以这样

错误显示:

Error: Cannot find module 'ejs'
   at Function.Module._resolveFilename (module.js:325:15)
   at Function.Module._load (module.js:276:25)
   at Module.require (module.js:353:17)
   at require (internal/module.js:12:17)
   at new View (d:\WebClient\webstormSpace\day6shuoshuo\node_modules\express\lib\view.js:78:30)
   at EventEmitter.render (d:\WebClient\webstormSpace\day6shuoshuo\node_modules\express\lib\application.js:569:12)
   at ServerResponse.render (d:\WebClient\webstormSpace\day6shuoshuo\node_modules\express\lib\response.js:961:7)
   at exports.showIndex (d:\WebClient\webstormSpace\day6shuoshuo\routes\router.js:7:9)
   at Layer.handle [as handle_request] (d:\WebClient\webstormSpace\day6shuoshuo\node_modules\express\lib\router\layer.js:95:5)
   at next (d:\WebClient\webstormSpace\day6shuoshuo\node_modules\express\lib\router\route.js:131:13)


原因:没有安装ejs模块

解决办法:

cmd进入到项目目录,安装ejs:npm install --save ejs

就行了!!






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值