How do I run a node.js function from an html page without using ajax/using express? - node.js

I have been having trouble with ajax, and I just want my html to pass a function when a button is passed, but that function is in a node.js file.
How could I do this without AJAX, and if AJAX is required, could I be linked code?

Create index.js file and attach it to your html (index.html) file.
Then in html file if you are submitting form then pass onClick=functionName() as attribute in <form> tag.
In index.js file, you can add functionName function and that function works as your frontend function.
Use axios or inbuild request to fetch data from that function via endpoint.

Related

Purpose of returning HTML as a Node response

What is the point of returning HTML as a response from a Node.js server?
I understand templating engines like Pug allow you to generate dynamic HTML using variables.
What I don't understand is why you wouldn't simply return JSON as a response, then use what is returned to render something based on that. I use React for example, so I could render render some JSX dynamically instead

Passing Data from Node-Express backend to Vue component

I'm still learning Vue.js 2 so I do apologize if this question is a bit silly. I'm building an application using MongoDB, Node, Express and Vue. Normally I'd use a template engine such as Ejs where data passed through Express's res.render method can be readily captured in the template.
Is there a similar way to pass data from backend to the root Vue component? For example, normally, a get request fetches some data from Mongodb, express will render the template file and pass data to it.
app.get("/gallery/:id", function(res, req) {
var id = req.params.id;
database.findById(id, function(err, data) {
....
res.render("home", data);
}
});
Now my root Vue application is attached to html file. I'd like to be able to dynamically render the app with data returned from the database.
I've built an app before that streams data from an api and passed it to my Vue component via socket but I feel like using a socket in this case is unnecessary.
Use http. What's the problem? You can use XmlHttp, or a lot of folk seem to be using Axios. Trigger the call in the onload of your page, or use one of the vue lifecycle hooks. The very good vue docs don't have much to say about how and when to do this. We do it in the onload of the page, and we instantiate vue when the request for page data returns.
The question has already been answered, but I wanted to share my approach. I've been experimenting with rendering an object server side using res.render(), putting it in a div where display: none, and then grabbing the object on the client and passing it to Vue's data attribute.

node.js - use template engine to return html

So i have a node.js app that's using express and ejs to render templates. I know that by using res.render() in my app object i can send a rendered response. But is there any way to just use the render engine to get the output'ed html?
Just had to look at the expressjs documentation for the Response object. Response.render is define like that: res.render(view [, locals] [, callback]). By supplying a callback function in the form of function(err, html), the render engine will call your callback function with the html string instead of sending it directly to the server.

create html dom element in front end using node js

I'm new to node js. Is that possible to create a html dom element on front end(index.html) using node js. If I insert any data into DB means then I want to create an html dom element like "li" (list) to show the inserted data in my index.html file.
Creating a html dom element in an existing html document is possible with javascript and whatever is your backend. You just use document.createElement() or whith jquery $('mytag').append('newTag). You can send JSON or (XML) from your server: res.json({foo: 'bar') with expressjs for example. Then from the client you make an AJAJ (AJAX) call to the route of your backend. I think what you are trying to do is a Single Page Application.

Render EJS and save it to Redis

In my Node-Express app I'm using Postgre as main data and Redis as cache system.
When a visitor request an url, my app request the json to Redis. If isn't available, make a request to Postgre, save the json to Redis and then render the EJS template. The next request to the same url, I get the json from Redis and render the template.
Now I want to save to Redis the full rendered template instead of json to save CPU usage. I tried this trick but it did not work:
Render ejs file in node.js
Any ideas or suggestions how to do it ??
Thanks in advance for your help.
The Express API reference shows that res.render() accepts a callback as the last argument. That callback is called with (err, html), so just pass in a callback and store the (string) html into redis.

Resources