I made a post request. I want to perform some operations and to send back data to the client-side based on which my frontend will change. How to do that??
CodePudding user response:
You can simply get a response to the post request after you make the changes. If the changes take too much time and cause a timeout, There are two possible ways to proceed:
- Set a timeout in frontend and make a get request to fetch the data.
- Look into socket.io. (This is a better way of handling things as you can set a listener on frontend to receive any data from backend.)
CodePudding user response:
In the server side you can write
var express = require('express');
var app = express();
app.listen('3000')
app.post("/getdata", (req, res) => {
res.send(//any json data)
})
in your client side you can write
var output = false
fetch("your server link or if you are on local server then
http://localhost:3000/getdata", {method: 'POST'})
.then(data => {return data.json()}).then(data => {output = data})
now you have your result in var output of your client side webpage