How to serve static webpage from falcon application?

I am new to python and therefore a falcon. I started developing the RESTful API and the falcon is still great for it. There is another requirement to serve a static web page, and I do not want to write an application or create a server for this.

Is it possible to serve a static web page from a falcon application?

+4
source share
4 answers

You can better manage the routes for your static file as follows:

import falcon
class StaticResource(object):
    def on_get(self, req, resp, filename):
        # do some sanity check on the filename
        resp.status = falcon.HTTP_200
        resp.content_type = 'appropriate/content-type'
        with open(filename, 'r') as f:
            resp.body = f.read()


app = falcon.API()
app.add_route('/static/{filename}', StaticResource())
+4
source

, , . , nginx Falcon nginx ( API Falcon).

, Falcon. , :

import falcon


class StaticResource(object):
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.content_type = 'text/html'
        with open('index.html', 'r') as f:
            resp.body = f.read()

app = falcon.API()
app.add_route('/', StaticResource())

, URL- , .

+7

:

api.add_static_route('/foo', foo_path)
api.add_static_route('/foo/bar', foobar_path)

+4

Source: https://habr.com/ru/post/1624529/


All Articles