How to route using XMLHttpRequest (js) and "pure" NodeJS? | SoloLearn: Learn to code for FREE!

+4

How to route using XMLHttpRequest (js) and "pure" NodeJS?

I am setting up a server using Node and hosting a HTML file "index.html" with "script.js" on localhost. My client-side code looks like: https://code.sololearn.com/WP3yos46EDwb/#html And, I'm handling the ajax request using my server-side script "server.js" which I've learnt from https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework#Example - It is really a good source :) After doing all these things, my hosted site runs very well. But, the only problem I face is routing programmatically. When, I type localhost:8125/login.html - it routes exactly as I wished but, when I try the same through my ajax request. It does everything, EXCEPT routing :| Please help me to solve this problem. Thanks in advance!

12/25/2018 7:00:32 AM

@vrintle

6 Answers

New Answer

+2

You can use ajax to fetch some resources or information from the server and put it dynamically in current page. For example changing or updating a datalist. But if you want an entire page, just use link

+2

Why not make it as a link ?

+2

Thanks Taste, it works! But I want to learn AJaX and so I was trying to route using AJaX. Anyways, if AJaX isn't the proper method to route, then I can use links.

+2

@Taste, can I bother you once more? I need help with redirection (changing the URL of web page programmatically, similar to routing) using Node.js In case of redirection, I can't use links, so how can I achieve it?

+2

This one if i remember correctly, i've been using express for too long 😂 res.writeHead(statusCode,{"location":"dest"}) statuscode should be 301 or 302 for temporary redirct or permanent

+2

@Taste Thanks for your help :)