NodeJS,如何使用Express 4呈现静态HTML? [英] NodeJS, how to render static HTML with Express 4?

查看:351
本文介绍了NodeJS,如何使用Express 4呈现静态HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用express 4渲染HTML文件,我写这样的东西,但是效果不好.

  app.route('/form1').get(function(req,res){
        res.sendfile('./public/danial/forms/form1.html');
  });

此代码可以发送HTML文件,但是它恰好发送HTML文件,而不发送HTML文件需要它们的CSS或js文件,这是日志:

   GET /form1 304 2.743 ms - -
   GET /css/bootstrap.css 404 2.284 ms - -
   GET /css/bootstrap-theme.css 404 2.193 ms - -
   GET /css/bootstrap-switch.css 404 2.226 ms - -
   // and many others

我需要做这样的事情:

   app.get('/', function(req, res) {
      res.render('index.html');
   });

我该如何解决?

(很多其他问题是关于快递3的,我找不到答案)

最后,我自己找到了答案.我使用ejs模板,将HTML文件的扩展名更改为ejs,并渲染它们和所需的文件我使用公用文件夹.

I need to render HTML files in express 4, I write something like this but not works good.

  app.route('/form1').get(function(req,res){
        res.sendfile('./public/danial/forms/form1.html');
  });

This code can send the HTML file ,but it exactly send the HTML file and don't send css or js files that HTML file need them ,this is the logs :

   GET /form1 304 2.743 ms - -
   GET /css/bootstrap.css 404 2.284 ms - -
   GET /css/bootstrap-theme.css 404 2.193 ms - -
   GET /css/bootstrap-switch.css 404 2.226 ms - -
   // and many others

I need to do something like this:

   app.get('/', function(req, res) {
      res.render('index.html');
   });

how can I fix it?

(many other questions are for express 3 and I can't find answer)

解决方案

Finally I myself find my answer.I use ejs template and change my extension of HTML files to ejs and render them and for needed files I use public folder.

这篇关于NodeJS,如何使用Express 4呈现静态HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆