Render raw HTML

前端 未结 11 1168
自闭症患者
自闭症患者 2020-12-23 18:48

I want to render raw .html pages using Express 3 as follows:

server.get(\'/\', function(req, res) {
    res.render(\'login.html\');
}

相关标签:
11条回答
  • 2020-12-23 18:53

    If you don't actually need to inject data into templates, the simplest solution in express is to use the static file server (express.static()).

    However, if you still want to wire up the routes to the pages manually (eg your example mapping '/' to 'login.html'), you might try res.sendFile() to send your html docs over:

    http://expressjs.com/api.html#res.sendfile

    0 讨论(0)
  • 2020-12-23 18:58

    After a fresh install of the latest version of Express

    express the_app_name
    

    Creates a skeleton directory that includes app.js.

    There is a line in app.js that reads:

      app.use(express.static(path.join(__dirname, 'public')));
    

    So a folder named public is where the magic happens...

    Routing is then done by a function modeled this way:

    app.get('/', function(req,res) {
          res.sendfile('public/name_of_static_file.extension');
        });
    

    *Example:* An index.html inside the public folder is served when invoked by the line:

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

    As far as assets go: Make sure the css and javascript files are called from the folder relative to the public folder.

    A vanilla Express install will have stylesheets, javascripts, and images for starting folders. So make sure the scripts and css sheets have the correct paths in index.html:

    Examples:

    <link href="stylesheets/bootstrap.css" rel="stylesheet">
    

    or

    <script src="javascripts/jquery.js"></script>
    
    0 讨论(0)
  • 2020-12-23 19:02

    After years a new answer is here.

    Actually this approach like skypecakess answer;

    var fs = require('fs');
    
    app.get('/', function(req, res, next) {
        var html = fs.readFileSync('./html/login.html', 'utf8')
        res.send(html)
    })
    

    That's all...

    Also if EJS or Jade will be used the below code could be used:

    var fs = require('fs');
    
    app.get('/', function(req, res, next) {
        var html = fs.readFileSync('./html/login.html', 'utf8')
        res.render('login', { html: html })
    })
    

    And views/login.ejs file contains only the following code:

    <%- locals.html %>
    
    0 讨论(0)
  • 2020-12-23 19:04

    You can send file using res.sendFile(). You can keep all html files in views folder and can set path to it in options variable.

    app.get('/', (req, res)=>{
        var options = {  root: __dirname + '/../views/' };
          var fileName = 'index.html';
          res.sendFile(fileName, options);
    });
    
    0 讨论(0)
  • 2020-12-23 19:05

    You can render .html pages in express using following code:-

    var app = express();
    
    app.engine('html', ejs.__express);
    

    And while rendering, you can use following code:-

    response.render('templates.html',{title:"my home page"});
    
    0 讨论(0)
  • 2020-12-23 19:07

    Have you tried using the fs module?

    server.get('/', function(req, res) {
        fs.readFile('index.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }
    
    0 讨论(0)
提交回复
热议问题