如何使用 Express 提供部分动态的 HTML 页面?

问题描述 投票:0回答:4

好吧,这是我的问题:我有一个我很满意的 HTML 页面,但我希望让它动态化。我在 Express 中使用 Node,我想知道是否有任何方法可以修改然后呈现纯 HTML。我不会使用 Jade 或任何其他模板引擎。

我的服务器.js:

var http = require('http');
var express = require('express');
var app = express()

var port = 3000;
var api_router = express.Router();

....

api_router.route('/webm/test/')
.get(function(req, res){
    res.sendFile(__dirname + "/test.html")
})

app.use('/api/', api_router);
app.listen(port);
console.log("NodeJS Backend API running.");

目前这不起作用(没有找到 HTML 的模板引擎)。它也不能满足我的需求:我希望根据 GET 请求在标签中设置“src='blah.webm'”。

这是我要修改的页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Index</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="../stylesheet.css">
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>

    <body>
        <div id="header">
            <a href="../index.html"><p>../</p></a>
            <p>TrentV.net : Trent VanSlyke</p>
        </div>
        <div class="container" style="display: flex">
            <video id="player" src="CUSTOMIZE ME" controls></video>
            <div id="related">

            </div>

            <script src="webm.js" type="text/javascript"></script>
        </div>
    </body>
</html>
javascript html node.js express
4个回答

0
投票

然后使用ejs 制作一个文件夹模板并在其中添加 registermessage 文件夹,并放置 html.ejs 用于 html 文件,style.ejs 用于 css 和 text.ejs 用于文本响应。

var templateDir = path.join(__dirname, "../../../", 'templates', 'registermessage')

    var resetpassword = new EmailTemplate(templateDir)
        //var user = {name: 'Joe', pasta: 'spaghetti'}

// the dynamic variable to replace

    var locals = {

        message: message,
        email: email

    };


    resetpassword.render(locals, function (err, temp) {
        if (err) {
            console.log("error" + err + "results" + temp);
            //  console.log( temp.html);
            res.send(temp.html);
            //next(error, null);
        } else {
            //console.log(temp.html)
            res.send(temp.html);
            //  next(null, "email sent");
        }

    })
}

<%message %> 会动态替换 html.ejs

<html lang="en" ng-app="xenon-app">
<head>
</head>
<body class="page-background" >
<div class="login-container" >

      <!--  Your email id  email -->       
      <p style="font-family: Verdana, Geneva, sans-serif;font-size:13px;"><%=message %></p>

</div>
</body>

</html>

0
投票
enter code here

[1]使用hbs模板引擎

点击查看:https://www.npmjs.com/package/hbs


-1
投票
You can use angular in node so that you can use like this ...
 make 
      <!DOCTYPE html>
        <html lang="en" ng-app="xenon-app">
        <head>
            <meta charset="utf-8">

           <script src="/assets/js/angular.js"></script>
            <script> 
             var app = angular.module( "xenon-app", [] );
             app.controller( "resetPasswordController",function($scope,$http,) {

        //some http calls of node and render that data in the page by $scope

    $http({
      method: 'GET',
      url: 'http://localhost:3000/someUrl'
    }).then(function successCallback(response) {
        // this callback will be called asynchronously

       $scope.name=response.name;
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });





        });

        </script>
        </head>
        <body ng-controller="resetPasswordController">
        <p>
        {{name}}
        </p>
        </body>
        </html>


        and render this page by
         res.sendFile(__dirname + "/test.html")
© www.soinside.com 2019 - 2024. All rights reserved.