【问题标题】:Node.js Express not rendering html viewNode.js Express 不呈现 html 视图
【发布时间】:2015-06-04 18:42:05
【问题描述】:

所以我试图按照教程使用 node.js 作为 wordpress 网站的前端

这个http://www.1001.io/improve-wordpress-with-nodejs/

这是来自 server.js 的代码

var frnt = require('frnt');
var fs = require("fs");
var path = require("path");
var express = require('express');
var app = express();
var doT = require('express-dot');

// Define where the public files are, in this example ./public
app.use(express.static(path.join(__dirname, 'public')));

// Make sure this is set before the frnt middleware, otherwise you won't
// be able to create custom routes.
app.use(app.router);

// Setup the frnt middleware with the link to the internal server
app.use(frnt.init({
    proxyUrl: "http://localhost:8888/frnt-example/wordpress", // The link to your wordpress site
    layout: false // We simplify this example by not using layouts
}));

// define rendering engine
app.set('views', path.join(__dirname, "views"));
app.set('view engine', 'html' );
app.engine('html', doT.__express );

// respond with "Hello World!" on the homepage
app.get('/', function (req, res) {
  res.send('./views/index.html');
});

app.listen(8080); // listen to port 8080

它不断输出以下内容

./views/index.html

而不是渲染html?

【问题讨论】:

    标签: node.js wordpress express


    【解决方案1】:

    我从来没有使用过 frnt,但是 res.send 发送了一个字符串,所以这并不奇怪。

    查看发送文件内容的 res.sendfile。

    【讨论】:

      【解决方案2】:

      我更喜欢使用 ejs ,就像 html 一样,只需将 index.html 编辑为 index.ejs 1-安装ejs模块npm install ejs 2- 在 app.js 中添加这个

      app.set('views', path.join(__dirname, 'views'));
      app.set('view engine', 'ejs');
      

      并使用渲染 index.ejs

      app.get('/', function (req, res) {
        res.render('index'); // or res.render('index.ejs');
      });
      

      res.send() // 它只发送一个字符串而不是文件

      希望有用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多