有什么res.render做,HTML文件是什么样的?

我的最终目标是将任意逗号分隔数从文本文件加载到HTML文件中(例如)。我只能推断出一个视图是HTML文件,而回调给了该HTML文件。

这是文档:http://expressjs.com/api.html

现在,从我发现的某些示例代码中给定上下文,使用ejs(嵌入JavaScript)<%%>

但是,如果我可以添加,我只是不称职还是真的很模糊,并假设读者知道一切?我怎么能自己弄清楚这一点?是否有任何官方文件,因此我可以完全了解使用,优势和陷阱?


Edit 1

我只想补充说我有很多时间学习node.js。是我还是一般文档真的模糊?除了上面的糟糕解释外,参数或返回值没有类型规格。


Edit 2

让我问您在代码上方的一些更具体的问题。

实际上orders.ejs文件在views/orders.ejs。该代码如何指它?

HTML摘录:

<tbody>
  <% for(var i=0; i<orders.length; i++) {%>
     <tr>
       <td><%= orders[i].id %></td>
       <td><%= orders[i].amount %></td>
       <td><%= orders[i].time %></td>
     </tr>
     <% } %>

和JS。请参阅 /订单:

  // Define routes for simple SSJS web app. 
// Writes Coinbase orders to database.
var async   = require('async')
  , express = require('express')
  , fs      = require('fs')
  , http    = require('http')
  , https   = require('https')
  , db      = require('./models');

var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 8080);

// Render homepage (note trailing slash): example.com/
app.get('/', function(request, response) {
  var data = fs.readFileSync('index.html').toString();
  response.send(data);
});

// Render example.com/orders
app.get('/orders', function(request, response) {
  global.db.Order.findAll().success(function(orders) {
    var orders_json = [];
    orders.forEach(function(order) {
      orders_json.push({id: order.coinbase_id, amount: order.amount, time: order.time});
    });
    // Uses views/orders.ejs
    response.render("orders", {orders: orders_json});
  }).error(function(err) {
    console.log(err);
    response.send("error retrieving orders");
  });
});

// Hit this URL while on example.com/orders to refresh
app.get('/refresh_orders', function(request, response) {
  https.get("https://coinbase.com/api/v1/orders?api_key=" + process.env.COINBASE_API_KEY, function(res) {
    var body = '';
    res.on('data', function(chunk) {body += chunk;});
    res.on('end', function() {
      try {
        var orders_json = JSON.parse(body);
        if (orders_json.error) {
          response.send(orders_json.error);
          return;
        }
        // add each order asynchronously
        async.forEach(orders_json.orders, addOrder, function(err) {
          if (err) {
            console.log(err);
            response.send("error adding orders");
          } else {
            // orders added successfully
            response.redirect("/orders");
          }
        });
      } catch (error) {
        console.log(error);
        response.send("error parsing json");
      }
    });

    res.on('error', function(e) {
      console.log(e);
      response.send("error syncing orders");
    });
  });

});

// sync the database and start the server
db.sequelize.sync().complete(function(err) {
  if (err) {
    throw err;
  } else {
    http.createServer(app).listen(app.get('port'), function() {
      console.log("Listening on " + app.get('port'));
    });
  }
});

// add order to the database if it doesn't already exist
var addOrder = function(order_obj, callback) {
  var order = order_obj.order; // order json from coinbase
  if (order.status != "completed") {
    // only add completed orders
    callback();
  } else {
    var Order = global.db.Order;
    // find if order has already been added to our database
    Order.find({where: {coinbase_id: order.id}}).success(function(order_instance) {
      if (order_instance) {
        // order already exists, do nothing
        callback();
      } else {
        // build instance and save
          var new_order_instance = Order.build({
          coinbase_id: order.id,
          amount: order.total_btc.cents / 100000000, // convert satoshis to BTC
          time: order.created_at
        });
          new_order_instance.save().success(function() {
          callback();
        }).error(function(err) {
          callback(err);
        });
      }
    });
  }
};

答案

Res.Render做什么,HTML文件是什么样的?

res.render()功能编译您的模板(请不要使用EJ),在此处插入当地人,并在这两件事中创建HTML输出。


回答Edit 2部分。

// here you set that all templates are located in `/views` directory
app.set('views', __dirname + '/views');

// here you set that you're using `ejs` template engine, and the
// default extension is `ejs`
app.set('view engine', 'ejs');

// here you render `orders` template
response.render("orders", {orders: orders_json});

因此,模板路径是views/(第一部分) +orders(第二部分) +.ejs(第三部分)===views/orders.ejs


无论如何,express.js文档对其所做的事情有益。它是API参考,而不是"如何使用node.js"书。

来自: stackoverflow.com