Home > Back-end >  data sending from server to client-side Nodejs
data sending from server to client-side Nodejs

Time:12-07

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:

  1. Set a timeout in frontend and make a get request to fetch the data.
  2. 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

  • Related