How to render a HTML page form NodeJs API?

纵饮孤独 提交于 2019-12-31 06:48:04

问题


**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.

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??**

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>

回答1:


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.

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

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

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

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



来源:https://stackoverflow.com/questions/51967761/how-to-render-a-html-page-form-nodejs-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!