REST backend within browser

I’m caressing the idea of developing a hobby web application, whose front end I’ll place in Firefox extension pages.

This may not be practical, but the idea is to implement suitable REST back ends as required (for each platform, possibly in different languages). But if I were to package the application as a Firefox extension (i.e., both front and back ends in JS), I wonder how I can implement a REST back end in Firefox if at all that’s possible. WebSocket server in WebWorkers/ServiceWorkers?

Conceivably, the front end will have a setting to pick a back end to connect to. For example, for a back end that I’d run on my host, I’d say localhost:8000/foo/bar. If an in-browser back end were possible, I’m not sure how that’d look like.

Any ideas?

You can’t run a traditional web backend within an extension. Packaging a “web app” as extension means you’ll have to write your backend very differently, using messaging and also running in a web context. You can also do a service worker based approach, I think.

1 Like

It is certainly possible to implement a REST backend in a Firefox extension using JavaScript. One way to do this is to use WebSockets or Service Workers to create a server that listens for incoming HTTP requests and sends back appropriate responses.

Here are a few steps you can follow to get started:

Create a new Firefox extension using the WebExtensions API. This will give you a basic structure for your extension, including a manifest.json file that defines the extension's permissions and background scripts.

In your extension's background script, create a server using either WebSockets or Service Workers. This server should listen for incoming HTTP requests and send back appropriate responses.

For each REST endpoint you want to create, define a corresponding route in your server. For example, you might define a route at /foo/bar that returns some data when it receives a GET request.

In your extension's frontend, use JavaScript's fetch function or a library like axios to send HTTP requests to your backend.

Keep in mind that while it is possible to create a REST backend in a Firefox extension, it may not be the most practical solution depending on your needs. For example, if you want to run the backend on a separate server, it might be easier to implement it in a different language. However, if you just want to create a simple backend for a hobby project, implementing it as a Firefox extension can be a fun and interesting way to learn more about extension development.