如何通过NodeJs API呈现HTML页面? [英] How to render a HTML page form NodeJs API?

查看:58
本文介绍了如何通过NodeJs API呈现HTML页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**运行http服务器后,我尝试访问URL- http://127.0.0.1:8080 -但是我得到的不是我写的每次尝试时,都会运行"Node.js v8.11.4/ecstatic服务器正在运行@ 127.0.0.1:8080".

**After running http-server, I try to get to URL- http://127.0.0.1:8080 - but instead of what I write, I get "Node.js v8.11.4/ ecstatic server running @ 127.0.0.1:8080"` every time I try.

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

But index.html file opens when clicked directly from the button, shows chrome as default browser and any changes made in coding like adding an email, password or changing the background color, it catches it accordingly. How can I solve this problem??**

这是index.html-

Here is the code for 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页面的端点.每个网址都是向服务器发出请求的API.我们在浏览器上输入的网址是GET API.

Looks to me like you are not able to find end-point which respond your HTML page. Every url is an API which make request to server. The url which we type on browser are GET API.

您的 http://localhost:8080

Your http://localhost:8080 and http://127.0.0.1:8080 are API with end-point as /.

如果您使用的是expressJs,则寻找类似这样的内容:

If you are using expressJs then look for something like this:

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.

Note your function should be app.get() as you wont be able to call app.post from browser.

您需要找出哪个响应HTML页面的端点.

You need to find out end-point which response HTML page.

这篇关于如何通过NodeJs API呈现HTML页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆