Handling a response from node.js without rendering it? - node.js

I am returning a simple response in node.js with res.send() in response to a POST request. My problem is that I want the client to stay on the same page. Currently the client just gets taken to a blank page that has the contents of res.send() written on it. But I want the client to update it's current page (the page from which the POST request was sent) instead of displaying the response. Is there a way to do this?

You need to change the action of your HTML form using jquery, angular, or backbone (etc..) The page change is the default functionality of a html form submission, and this cannot be resolved by node.js.

Related

Is there a difference in a fetch request body when its created from a user typing the url and when its created from a clicked link?

I have a project that uses React but server rendered. When a client requests an initial page of the website by writing the URL in the search field, the server.js uses renderToString() to stringify the react App.js (with some initial data added) and send it to the client along with the bundle.js and css. From this point onwards, React takes over, the client will navigate through the app without needing an initial data anymore. Whenever they navigate to a different component, componentDidMount() will request for the corresponding data from the same server.js.
The problem is that I cannot distinguish between a GET request from a componentDidMount() and from the user typing the URL in the search bar. This is crucial for knowing when to send a new markup with an initial data and when to just send a response object.
Right now I am using a very crude method of attaching a querystring in the GET request from the componentDidMount() to identify that it requires a non-initial data/that the request is not an initial request on entering the website.
This method is very messy as in one instance for example, upon refreshing, the non-initial query stays in the url but since its refreshed, it throws all the cached react app, and it then receives the non-initial data displayed nakedly in the browser.
Is there a better way of doing this? Maybe there is an attached information in the fetch get request that shows where the GET request is generated (whether from a clicked link or a typed URL)?

How do i put the request result inside an html in express?

im trying to make a website using express, and in this website i request some data from an external API.
So, i have an html where i "send" the request. How do i take that parameters for the request into the server, and then the response to the html or at least the js linked to that html?
To this point, i already tested how to add an html with a js linked to it, and it worked, so now i have to make the rest of the web concept, that is request data from the API.
Sorry if i dont have the code, but im still making it and i have this big issue that i cant resolve.
Thanks for your time and advice anyways
You have two choices.
Either you make an ajax request to the api from the front-end or in the back-end and render the result.
You can also make a request from the front-end, send the result to the back-end and have express send a different response.
No code attached as your question is very generic.

node js asking browser to reload the page

I want to reload the page after some validations placed at the server side fails.. i have already tried with res.redirect res.location and location.reload() .
I need to place the reloading logic on the server side in nodeJS.
So whenever there is a request from client side this validation gets triggered and if the validation fails i am destroying the window user object but i am unable to reload the page...it's stays on the same same and then all services starts failing..because of user object check now..
If you do an ajax call you need to reload in the client side browser code based on http response.
if it's a form post you can render whatever html you want back to the client.
A way to build this in your ajax framework would be to: In serverside you could redirect and then clientside, have the code that looks like this (warning this is just psuedo code):
if(response.httpStatusCode == 301 or 302)
window.location(response.headers.location)

how the internet connecting all clints via server?

can any explain me about how the server responds if we click the button in browser using PHP script then which situation we use JAVASCRIPT and which situation we have to use PHP .
If you're asking about how it works, I'll suggest to google about HTTP protocol, POST and GET methods.
Usually, you use JavaScript to send request to a server when you want it to be AJAX, ie when you want to send request to server and receive response without reloading page. And you use PHP when you have form in your HTML (like login or registration form), and you send it to server in one request to go to another page

How multiple requests happens from a web browser for a simple URL?

While trying to serve a page from node.js app I hit with this question. how multiple files are serving from a server with a simple request from user?
For eg:
A user enters www.google.co.in in address bar
browser make a request to that url and it should end there with a response. But whats happening is, few more requests are sending from that page to server like a chain.
What I am thinking now is, how my web browser(chrome) is sending those extra requests... Or who is prompting chrome to do it? and ofcourse, how can I do the same for my node.js app.
Once chrome gets the html back from the website, it will try to get all resources (images, javascript files, stylesheets) mentioned on the page, as well as favicon.ico That's what I think you are seeing
You can have as many requests as you want, from the client side (browser), using ajax. Here's a basic example using jQuery.get, an abstraction of an ajax call:
$.get(
"http://site.com",
function(data) { alert(data); }
);

Resources