How do I serve partially dynamic HTML pages with Express?

32,562

You can use express view templating like described here

Share:
32,562
Trent
Author by

Trent

Updated on July 05, 2022

Comments

  • Trent
    Trent almost 2 years

    Alright, here's my issue: I have an HTML page I am quite happy with, but I wish to make it dynamic. I am using Node with Express, and I was wondering if there was any way to modify and then render plain HTML. I will not be using Jade or any other template engines.

    My server.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.");
    

    Currently this does not work (no templating engine for HTML found). It also does not fulfill my needs: I wish to set the "src='blah.webm'" in a tag depending on the GET req.

    This is my page I wish to modify:

    <!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>
    
  • Trent
    Trent about 8 years
    Angular is a bit beyond the scope of this project.
  • Trent
    Trent about 8 years
    While not exactly what I wanted, EJS works just fine for me now. Accepted. Muchas gracias :)
  • yeiniel
    yeiniel about 8 years
    De nada. Que tengas un buen dia. Saludos
  • Himanshu sharma
    Himanshu sharma about 8 years
    then only way to use ejs i will provide u code of it
  • Rahul Kumar
    Rahul Kumar almost 6 years
    Still using the ejs templating engine
  • Himanshu sharma
    Himanshu sharma almost 4 years
    Check above answer for ejs.