Node.js - EJS example

后端 未结 3 517
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 15:14

I am trying to use Embedded Javascript renderer for node. I installed it using npm, as given here: https://github.com/visionmedia/ejs

And I have the following code, but

3条回答
  •  梦如初夏
    2021-02-05 15:50

    You need to send something to the response. From the connect hello-world

    var connect = require('../../lib/connect');
    
    var server = connect.createServer(function(req, res){
      var body = 'Hello World';
      res.writeHead(200, {
          'Content-Type': 'text/plain'
        , 'Content-Length': body.length
      });
      res.end(body);
    });
    
    server.listen(3000);
    console.log('Connect server listening on port 3000');
    

    So for your app you'll want to replace:

    function(req,res) {
     ejs.render('hi');
    }
    

    With something like:

    function(req,res) {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end(ejs.render('hi'));
    }
    

    Does that work?

提交回复
热议问题