nodejs send html file to client

后端 未结 3 1094
迷失自我
迷失自我 2021-01-30 17:10

I use this function to send html file to client, but in client I get nothing (blank page) without error. Something I wrong?, please help?

var express = require(\         


        
3条回答
  •  无人及你
    2021-01-30 17:52

    After years, I want to add another approach by using a view engine in Express.js

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

    Then, do that in your views/test if you choose res.render method at the above code (I'm writing in EJS format):

    <%- locals.html %>
    

    That's all.

    In this way, you don't need to break your View Engine arrangements.

提交回复
热议问题