How to render a HTML page form NodeJs API?

2019-03-04 14:22发布

问题:

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