node js asking browser to reload the page - node.js

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)

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 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

Cannot request Iron Router server route twice without refreshing the client

I am calling a meteor method, which generates a file using fs. I wait for a callback giving me the path where the file exists, and then I request the file with a server route. The code is very similar to this SO answer. I have also tried using createReadStream (as demonstrated here) instead of passing the file directly to response.write.
This all works well on the client's first click of my export/download button. However, if for some reason they want to click the button more than once, the file will get generated but the file will not get served by Iron Router. There are no errors on the client or server. If the user refreshes the client, then the feature will work again (once).
Why do I need to refresh the browser in order to request the same server route a second time? Am I doing something wrong?
Example Application
Does the URL change when they click the first download? If so, and the second route is the same, you will not get redirected as you are already there. If this is the case, can you use the router hooks to send the user back to the route they came from?

Handling a response from node.js without rendering it?

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.

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