• NodeJS/Express Pug unbuffered code on Event
    6 replies, posted
Hope there are somebody who is experienced in Node/Express. To keep it short, I have some calculations that are done through unbuffered code block. Lets say I have two variables a and b that are in input forms input(type='number', name='a') input(type='number', name='b') p= Summary() Now, I have a function called Summary that returns the result of the function into p function Summary() { return a+b; } Now, the inputs are inside in a form. After the form is submitted and a POST request is received, it will update the page with the summary of A and B. The question is, how do I do this dynamically? I tried doing something like input(type='number', oninput=Summary() name='a') input(type='number', oninput=Summary() name='b') p= Summary() Or even using "" or `` input(type='number', oninput=`Summary()` name='a') input(type='number', oninput=`Summary()` name='b') p= Summary() However nothing works. What gives? Alternative question: How do I dynamically call functions from server-side?
Why would you? What you're doing where clientside JS can't solve it?
I think by "dynamic" the OP means he want's the page to update seamlessly without a refresh? The easiest way would be to do everything client side like the previous poster mentioned. You say you have a Summary function defined, but where? I assume on the server side from how you're describing your project. First though, when you say it doesn't work, what happens when it "doesn't work?". Do you get an console log error message? I'd imagine you might get a function is undefined message. If you want things to update seamlessly, then you need to have the Summary() function defined in a .js file and imported as a script in your pug file. Then call the function in quotes oninput. Hope this helps. Let me know if you have questions
This is a very simplified example of what I want to do. My "calculations" are supposed to be done server-side since my employer atm stressed that he doesn't want to leave calculations exposed to client-side. The logic behind the calculations are pretty much his IP he is worried might get stolen or reverse-engineered, so it was stressed that it would be better to perform calculations serverside.
Makes sense give this requirement. If you want it done seamlessly, you'll have to make an ajax POST request to some defined express route. You'd have to pull in jQuery for something like this. So you'll still make a function on the client side, but in that function you'll make a network request that passes a and b as request body params to the server. See below for some code (it should be almost correct) function makeRequest(formData) { $.post('/calculate', { data: { a: formData.a, b: formData.b }, success: function (data) { // handle server response here by // manipulating the dom in some way }, error: function (error) { // handle error case } }); } Then, on the server side, you'd pass those params to the Summary function and return the result to the client.
Please don't pull in jQuery for something like this - function makeRequest(formData) { let args = FormData(); args.append('a', formData.a); args.append('b', formData.b); fetch('/calculate', { body: args.toString(), method: 'POST' }).then(function(data) { // do what you need to with data }).catch(function(err) { console.error(err); }); }
Agreed. Your solution is more idiomatic/better. I just tried to give a simplified explanation of the "moving" parts
Sorry, you need to Log In to post a reply to this thread.