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


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: And, I'm handling the ajax request using my server-side script "server.js" which I've learnt from - 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


6 Answers

New Answer


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


Why not make it as a link ?


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.


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


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


@Taste Thanks for your help :)