【问题标题】:Node.js not rendering html page from 'public/views' routeNode.js 不从“公共/视图”路由呈现 html 页面
【发布时间】:2019-12-27 02:04:55
【问题描述】:

在从 index.html 执行 xml http 请求后,我正在尝试渲染一些 html,但服务器没有渲染我的 html 页面,即使日志似乎工作正常:

<input type="button" id="response_2" value="No, let me create one :0" class="button" style="width: 100%; height: 100%" onclick="createAccount()">

这是在 index.html 中找到的按钮

function createAccount()     {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "/createAccount");
    xhttp.send();
}

这是javascript函数。也可以在 index.html 文件中找到,该文件在按下“response_2”按钮时执行

    var express = require('express');
    var app = express();
    var http = require('http');
    var port = 3000;
    var bodyParser = require('body-parser');

    var httpServer = http.createServer(app);
    httpServer.listen(5500);

    app.use(express.static(__dirname + '/public'));
    app.set('views', __dirname + '/public/views');
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');

    app.use(bodyParser.urlencoded({
    extended: true
    }));
    app.use(bodyParser.json());

    app.listen(port, () => console.log(`app listening on port ${port}!`));

    app.get('/', function (req, res) {
        console.log("someone here...");
        res.render('index.html');
    });

    app.get('/createAccount', function (req, res) {
        console.log("here!");
        res.render('createAccount.html');
    });

    app.get('/login', function (req, res) {
        res.render('login.html');
    });

    app.get('/forgotCredentials', function (req, res) {
        res.render('forgotCredentials.html');
    });

这是node.js服务器的配置

【问题讨论】:

    标签: javascript html node.js server


    【解决方案1】:

    在我看来,您缺少一些代码。
    在您的客户端示例中,您有 createAccount 函数,该函数将 XMLHTTPRequest 触发到服务器。

    function createAccount() {
        var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "/createAccount");
        xhttp.send();
    }
    

    但是它错过了它应该对请求的response 做的部分。将onreadystatechange 方法添加到xhttp 实例,如下所示。

    function createAccount() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if(xhttp.readyState === 4 && xhttp.status === 200) {
                console.log(xhttp.responseText);
            }
        }
        xhttp.open("GET", "/createAccount");
        xhttp.send();
    }
    

    只要请求的readyState 发生更改,就会调用onreadystatechange。如here on MDN. 所述。在函数中,您可以控制必须对结果执行的操作。

    据我所知,console.log(xhttp.responseText); 应该将createAccount.html 页面作为字符串输出。

    我的猜测是,您应该使用链接的href 导航到/createAccount

    <a href="/createAccount" title="Create account">Create account</a>
    

    【讨论】:

      【解决方案2】:

      你的代码

      app.get('/createAccount', function (req, res) {
        console.log("here!");
        res.render('createAccount.html');
      });
      

      这样当有人在浏览器中访问/createAccount URL 时,他们会看到您告诉服务器返回的页面。您的 createAccount() 函数不会在浏览器中打开此 URL,它只会发出呈现该页面的请求,因此服务器会返回该页面但您从未真正访问过它。

      你为什么不直接使用

      <a href='/createAccount'>
        <button>No, let me create one :0</button>
      </a>
      

      在你的页面而不是那个输入元素中?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多