简体   繁体   中英

set attribute 'src' to an element in html using nodejs

I use 'express' nodejs and using this code to send url into text

<form method="GET" action='/download' class="my-5">
            <div class="form-group">
                <label for="exampleInputEmail1"
                    >Link:</label
                >
                <input
                    type="text"
                    class="form-control"
                    placeholder="Enter URL"
                    name="url"
                />
            </div>
            <button type="submit" class="btn btn-primary">Download</button>
        </form>

in my project I receive

    const express = require("express");
const app = express();
app.set('view engine', 'ejs');

app.get("/", (req, res) => {
    return res.render("index");
});
app.get("/download", async(req, res) => {
    console.log(req.query.url);
});


app.listen(3000, () => {
    console.log("Server is running on http://localhost:3000");
});

as you can see I use get '/download' to receive url from input and doing some analazing to output new url

my quation is: how can I set this new url to an arttibute 'src' for element (such as img) in homepage without loading new page?

I need only put this new url to be value in 'src' for img in index.eje

app.get("/", (req, res) => {
    return res.render("index",
        {
            srcAttr: 'link to image'
        }
    );
});
<img src="<%= srcAttr %>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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