如何从 NodeJs API 渲染 HTML 页面?

2024-01-10

**运行http-server后,我尝试访问URL-http://127.0.0.1:8080 http://127.0.0.1:8080- 但我得到的不是我写的 每次我尝试时,都会显示“Node.js v8.11.4/ 欣喜若狂的服务器正在运行@ 127.0.0.1:8080”。

但是,直接从按钮单击时,index.html 文件会打开,将 chrome 显示为默认浏览器,并且在编码中所做的任何更改(例如添加电子邮件、密码或更改背景颜色)都会相应地捕获。我怎么解决这个问题??**

这是index.html的代码-

<!DOCTYPE html>
<html>
    <head>
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script>
            <link rel="stylesheet" href="css/style.css" />
            <script src="https://www.gstatic.com/firebasejs/5.4.0/firebase.js"></script>
    </head>

    <body class="bg-dark">

        <div id="login-card" class="card">
            <div class="card-body">
                <h1> Wallpaper App Admin </h1>

                <form id="login-form">
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" id="email" class="form-control" />
                    </div>
                    <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" id="password" class="form-control" />
                        </div>
                        <div class="form-group">
                            <button id="btn-login" type="button" class="btn btn-primary">Login</button>
                        </div>
                </form>
            </div>
        </div>
        <script src="js/app.js"></script>

        <script>

            firebase.auth().onAuthStateChanged(function(user){
                if(user){
                    window.location.href = "admin.html";
                }
            });

        </script>

        </body>
</html>

在我看来,您无法找到响应您的 HTML 页面的端点。 每个 url 都是一个向服务器发出请求的 API。我们在浏览器中输入的url是GET API。

Your http://本地主机:8080 http://localhost:8080 and http://127.0.0.1:8080 http://127.0.0.1:8080API 的端点为/.

如果您使用的是expressJs,那么寻找类似这样的东西:

app.get('/yourAPI/', (req, res) => {
    //res.render is function that send html page to browser
    res.render('htmlFile');
});
// So your page will be available on http:127.0.0.1:8080/yourAPI
// or http://localhost:8080/yourAPI

请注意,您的函数应该是 app.get(),因为您无法从浏览器调用 app.post。

您需要找出响应 HTML 页面的端点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 NodeJs API 渲染 HTML 页面? 的相关文章

随机推荐