简体   繁体   English

如何使用 node.js 更改前端 html

[英]How to change front-end html using node.js

So I want to make changes on my front-end html file from my node.js backend.所以我想从我的 node.js 后端对我的前端 html 文件进行更改。 How would I do so?我该怎么做? (Any suggestions with examples would be greatly appreciated) (任何带有示例的建议将不胜感激)

Here's where I would like to change it:这是我想改变它的地方:

router.post('/change', function(req, res){
    console.log(req.body.list);
    //modify html DOM here!!
    //preferably using jQuery
})

You can't modify from the node side in the way that I think your're trying to do.您不能以我认为您正在尝试做的方式从节点端进行修改。 You can send back a response to the client side, and on that response, you can trigger a function ( that you can write in JQuery ) that will change the DOM and save what needs to be changed in local storage ... here is an example ...您可以向客户端发送回响应,在该响应上,您可以触发一个函数(您可以在 JQuery 中编写),该函数将更改 DOM 并将需要更改的内容保存在本地存储中......这是一个例子 ...

//server.js
router.post('/change',function(req,res){

    // the message being sent back will be saved in a localSession variable
    // send back a couple list items to be added to the DOM
    res.send({success: true, message: '<li>New list item number 1</li><li>New list item number 2</li>'});
});

Here is the front end ...这里是前端...

//index.html
//body
<h1>Hello World</h1>
    <ul>
        <li>List item 1</li>
         //li items with class change will be changed on button click
        <li class='change'>List item 2</li>
        <button class="trigger">Trigger Change</button>
    </ul>

<script>

   //if we have data stored in the session variable, then use the data to change the DOM text
    if(window.localStorage.permanentData){
        $('li.change').replaceWith(window.localStorage.permanentData);
    }

    //change DOM function
    function changeDom(){
        //ajax call
         $.ajax({
                  url: 'http://localhost:8080/change',
                  method:'POST',
                  data: {list: "some info"}
                }).done(function(data){
                    //if we have a successful post request ... 
                    if(data.success){
                        //change the DOM &
                        //set the data in local storage to persist upon page request
                        localStorage.setItem("permanentData", data.message);
                        var savedText = localStorage.getItem("permanentData");
                        $('li.change').replaceWith(savedText);

                        return;
                    }
                }).fail(function(){
                   //do nothing ....
                    console.log('failed...');
                    return;
                });
        };

    //trigger change DOM  function
    $('.trigger').click(function(){
        changeDom();
    });

</script>

I recently ran into a similar problem, a solution I found was to use fs.readFile to open the original html file and then use cheerio library to alter html elements:我最近遇到了类似的问题,我找到的一个解决方案是使用fs.readFile打开原始 html 文件,然后使用cheerio库来更改 html 元素:

const express = require("express");
const app = express();
const cheerio = require("cheerio");
const fs = require("fs");
const https = require("https");

app.post("/", function(req, res) {
  var userInput = req.body.userInput;

  fs.readFile(index.html, "utf8", function(err, data) {
    if (err) throw err;

    var $ = cheerio.load(data);

    $(".some-class").text("returned user input: " + userInput);
    res.send($.html());
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM