Skip to content
Advertisement

Is it possible to receive http requests using vanilla js?

The title says it all.

Is it possible to receive http requests using vanilla JS?

I know the fetch API allow me to send them, but, as far as I know, there’s no way to receive them. If there is, how? Jquery?

Advertisement

Answer

JS itself has no mechanisms for input and output. It depends on the host environment to provide those.

When people say “Vanilla JS” they usually mean “Core JS + Web APIs as are made available in a <script> element in an HTML document” (where the host environment provided by a web browser).

These APIs provide no mechanism for listening for incoming network connections of any kind. You cannot run a web server inside a web browser.

If you want to do that then you’ll need an environment which can create network services, such as Node.js.


It is possible that your real problem can be solved some other way. You can listen for server pushed data using Web Sockets. In this case the JS running in the browser needs to connect to a Web Socket server provided by something else (often Node.js) and then that server can push messages to the browser. Most browser based instant messengering services use this technology.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement