Render raw HTML

前端 未结 11 1169
自闭症患者
自闭症患者 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 19:09

    What I think you are trying to say is: How can I serve static html files, right?

    Let's get down to it.

    First, some code from my own project:

    app.configure(function() {
        app.use(express.static(__dirname + '/public'));
    });
    

    What this means that there is a folder named public inside my app folder. All my static content such as css, js and even html pages lie here.

    To actually send static html pages, just add this in your app file

    app.get('/', function(req, res) {
      res.sendFile(__dirname + '/public/layout.html');
    });
    

    So if you have a domain called xyz.com; whenever someone goes there, they will be served layout.html in their browsers.

    Edit

    If you are using express 4, things are a bit different. The routes and middleware are executed exactly in the same order they are placed.

    One good technique is the place the static file serving code right after all the standard routes. Like this :

    // All standard routes are above here
    app.post('/posts', handler.POST.getPosts);
    
    // Serve static files
    app.use(express.static('./public'));
    

    This is very important as it potentially removes a bottleneck in your code. Take a look at this stackoverflow answer(the first one where he talks about optimization)

    The other major change for express 4.0 is that you don't need to use app.configure()

    0 讨论(0)
  • 2020-12-23 19:09
    app.get('/', function(req, res) {
      returnHtml(res, 'index');
    });
    
    function returnHtml(res, name) {
      res.sendFile(__dirname + '/' + name + '.html');
    }
    

    And put your index.html to your root page, of course you could create a /views folder for example and extend returnHtml() function.

    0 讨论(0)
  • 2020-12-23 19:10

    I wanted to do this because I'm creating a boilerplate NodeJS server that I don't want tied to a view engine. For this purpose it's useful to have a placeholder rendering engine which simply returns the (html) file content.

    Here's what I came up with:

    //htmlrenderer.js
    
    'use strict';
    
    var fs = require('fs'); // for reading files from the file system
    
    exports.renderHtml = function (filePath, options, callback) { // define the template engine
        fs.readFile(filePath, function (err, content) {
            if (err) return callback(new Error(err));
            var rendered = content.toString();
            // Do any processing here...
            return callback(null, rendered);
        });
    };
    

    To use it:

    app.engine('html', htmlRenderer.renderHtml);
    app.set('view engine', 'html');
    

    Source: http://expressjs.com/en/advanced/developing-template-engines.html

    Comments and constructive feedback are welcome!

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

    as the document says : 'Express expects: (path, options, callback)' format function in app.engin(...).

    so you can write your code like below(for simplicity, but it work):

    server
    .set('view options', {layout: false})
    .set('views', './../')
    .engine('html', function(path, options, cb) {
        fs.readFile(path, 'utf-8', cb);
    });
    

    of course just like 2# & 3# said, you should use express.static() for static file transfer; and the code above not suit for production

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

    First, the mistake you did was trying to use the express 2.x code snippet to render raw HTML in express 3.0. Beginning express 3.0, just the filepath will be passed to view engine instead of file content.

    Coming to solution,

    create a simple view engine

    var fs = require('fs'); 
    
    function rawHtmlViewEngine(filename, options, callback) {
        fs.readFile(filename, 'utf8', function(err, str){
            if(err) return callback(err);
    
            /*
             * if required, you could write your own 
             * custom view file processing logic here
             */
    
            callback(null, str);
        }); 
    }
    

    use it like this

    server.engine('html', rawHtmlViewEngine)
    server.set('views', './folder');
    server.set('view engine', 'html');
    


    Reference

    Official express 2.x to 3.x migration guide

    See 'Template engine integration' section in this url https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x

    0 讨论(0)
提交回复
热议问题