0

I've been trying to figure out a better way to push a variable from the backend to the frontend. Right now I do something like this.

I have a MVC-pattern, so when hitting the route

app.get('/fil', middleWare.isLoggedIn, user.fil)

... trough node does some querying the DB, and pass on the data.

exports.fil = async (req, res) => {

    try {

        faktura = await Lan.find().populate('client', 'namn kundnr')

        res.status(200).render('pages/createInvoice', {
            faktura: faktura
        });

    } catch (err) {
        return res.status(500).send({
            message: err.message || "Some error occurred while retrieving datas."
        });
    };
};

... it generates the page, with the help of EJS (i love EJS) and then pass it on to the client/user.

And in the .ejs-file that is served to the client/user I add the following

<script>
    var fakturor = <%- JSON.stringify(faktura) %>;
</script>

which then means that I use up the variable and work with it with JS.

And this is where my question pops up. Is this a good way to do it or is there any other way to handle it?

I guess one idea is to let the user to query the DB straight from the page, but in my case I believe it wouldn't actually be better for the user to do so (the user will reieve like 100 different rows that they will be able to filter and then download a file of)

But is there any other ways I could do this without the script-tag? Like i said, I guess a ajax-call from JS/the client could be used but could you do it any other way? Can EJS do it any other way?

4
  • 1
    use response.json and send a Json obejct containing the payload you want to send Commented Dec 18, 2018 at 10:58
  • @INDRESHKHANDELWAL, care to elaborate? response.json? Commented Dec 18, 2018 at 11:02
  • 1
    response.json() takes a json object and sends it the requested http request.So if lets say you want to send a variable x then it can be done in this way => response.json({x: value}) Commented Dec 18, 2018 at 11:06
  • Hmm. I´ll check that out. Thank you for the information. You don't happen to have a good link where I can read up on it? Commented Dec 18, 2018 at 11:15

1 Answer 1

1

ejs is used for static pages mainly, if you want to build a dynamic page I would look for a single page application framework like angular and react. if you still want to use ejs you can use ajax call to the server to load a variable from the DB. I would never query directly from Front end to DB because then you are not controlling the security of the server, always go through the BE.

  • also try to think if you really need a variable in the front end, can you solve your problem using rendering only?
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not really interested in building a SPA:s as I deem it to be overkill for my smaller projects. I could be wrong though. The idea of ajax-call trough the BE is what I've so far gathered as a possibility, and I guess I´ll check it out. The thing is that, I pass the array, then the user can do some changes to it and then "save" it, as an array. It then saves this to DB, but also copy the array > xml nd the user get prompted if the want to dload the file. Im not sure how I could solve the part where users can change stuff if theres is no array passed on from the BE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.