Using express without a template engine

前端 未结 4 1267
眼角桃花
眼角桃花 2021-01-17 23:59

Is it possible to create an express (node) application without the need for a template engine such as jade or ejs. I\'ve got a large final year project at university and i\'

相关标签:
4条回答
  • 2021-01-18 00:09

    If you only want to avoid learning another template language, you might want to give underscore templates a try. They're just javascript, which you're going to be learning anyway.

    documentcloud.github.com/underscore/#template

    You can set it up with:

    app.register('.html', {
        compile: function(str, options){
            var compiled = require('underscore').template(str);
            return function(locals) {
                return compiled(locals);
            };
        }
    });
    
    0 讨论(0)
  • 2021-01-18 00:25

    Is it possible to create an express (node) application without the need for a template engine such as jade or ejs

    Yes it is. You can just use HTML. Or just use EJS. EJS is a superset of HTML.

    I don't want to burden myself with having to learn a templating language too!

    You can learn a templating language in a day. It's really going to help you. Just do it. It's worth it.

    0 讨论(0)
  • 2021-01-18 00:28

    The best option right now is to use ejs (engine) and configure it to accept and render html:

    app.set('views', path.join(*__dirname*, 'views'))
    app.set('view engine', 'ejs'); // template engine
    app.engine('html', require('ejs').renderFile); // turn engine to use html
    

    Note: All your views or templates have the .html extension.

    0 讨论(0)
  • 2021-01-18 00:32

    Easiest way to do this would be to replace the default app.get('/')... line with the following. Then put all the magic in index.html. This will at least work quite well for a single page app.

    with the following

    app.get('/', function(request, response) {
    var readFile = "index.html";
    var fileContents = fs.readFileSync(readFile);
    
    response.send(fileContents.toString());
    });
    
    0 讨论(0)
提交回复
热议问题