简体   繁体   中英

path of socket.io doesnt working in the html script tag

I am not very much experienced in js and i am not able to understand why the script tag path doesnt work for me whereas i found everywhere that script but for me it doesnt work i am also new to these things not much experienced so if you found any error from my side please help me out.

desc:- i am working on a server client based project where i need the socket.io library and i have included all the things necessary for it to work

my project works when i try this

<script defer src="http://localhost:8000/socket.io/socket.io.js "></script>

but it doesnt work when i do like this (i saw this almost everywhere in the internet)

<script defer src="/socket.io/socket.io.js "></script>

++help i also want it to work(deploy) on heroku so please help so that it should not have default ports

any help will be highly appreciated

When you add "/socket.io/socket.io.js" , it'll try to load that file with your location.origin + "/socket.io/socket.io.js" and since you don't have a server running in the path location.origin , it'll fail loading the socket.io.js file.
But When you add the address along with the relative path, it becomes absolute and hence it'll just find the path you mentioned and fetch the socket.io.js file available there. ( since your server is running there with socket.io installed )

And for the second question, once after deploying your application on Heroku, you'll get a link, just join that link with "/socket.io/socket.io.js" and that will work.

I hope this answer helps you. Add a comment if you get any doubt about this. And mark the answer if you find it helpful so that others who are stuck with the same problem can also be benifisiary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM