How to serve static files from a different directory than the static path?

tornado static path
tornado file server
tornado serve html
tornado serve image
django serve static files in production
serving static files in express
django not serving static files
node serve static files without express

I am trying this:

favicon_path = '/path/to/favicon.ico'

settings = {'debug': True, 
            'static_path': os.path.join(PATH, 'static')}

handlers = [(r'/', WebHandler),
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()

But it keeps serving the favicon.ico that I have in my static_path (I have two different favicon.ico's in two separate paths, as indicated above, but I want to be able to override the one in the static_path).

Delete static_path from the app settings.

Then set your handler like:

handlers = [
            (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path_dir}),
            (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path_dir}),
            (r'/', WebHandler)
]

ExpressJS - Serving static files, . You need to enable it using the following built-in middleware. Serve static files from different folder than wwwroot folder in ASP.NET Core. You can configure middleware to serve static files from other folders along with default web root folder wwwroot. For example, we will server admin.html from the following admin folder and also test.html from wwwroot folder.

You need to wrap favicon.ico with parenthesis and escape the period in the regular expression. Your code will become

favicon_path = '/path/to/favicon.ico' # Actually the directory containing the favicon.ico file

settings = {
    'debug': True, 
    'static_path': os.path.join(PATH, 'static')}

handlers = [
    (r'/', WebHandler),
    (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()

Serving static files in Express, Learn how to serve static files from different folder than wwwroot folder in ASP.​NET Core 2. StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path​. See Deploying static files for proper strategies to serve static files in production environments. Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories ( STATICFILES_DIRS ) in your settings file where Django will

There are two ways to do it.
1. use static_url_prefix in settings.

e.g.

settings = dict(
    static_path=os.path.join(os.path.dirname(__file__), 'static'),
    static_url_prefix="/adtrpt/static/",
)
2. use custom handler

Append custom handler to handlers

handlers.append((r"/adtrpt/static/(.*)", MyStaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), 'static')}))

Then implemente your custom methods.

class StaticHandler(BaseHandler):
    def get(self):
        path = self.request.path
        print(path)
        self.redirect(BASE_URI + path)

Working with Django Templates & Static Files ― Scotch.io, I am trying this: favicon_path = '/path/to/favicon.ico' settings = {'debug': True, '​static_path': os.path.join(PATH, 'static')} handlers = [(r'/', WebHandler),  Serving Static Files via Express Middleware. We have already created the public folder from where we will serve static files. In this step we are going to use express.static middleware to serve static assets from the public folder. We will create images, CSS, JS folder and index.html file within the public folder and serve directly from public

How to serve static files in Flask, To serve static files such as images, CSS files, and JavaScript files, use the app from another directory, it's safer to use the absolute path of the directory that​  The from options declares where these static files are located. In this case it references our application ( :my_app ) as the target which will translate to its priv/static directory. If you instead want to serve your files from a different, custom directory, you can replace it with the path to that directory.

How to serve static files from another folder other than wwwroot , This page describes how you can serve these static files. In your templates, use the static template tag to build the URL for the given relative path using the putting our static files directly in my_app/static/ (rather than creating another my_app That is, by putting those static files inside another directory named for the  To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below: app.use('/static', express.static('public')) Now, you can load the files that are in the public directory from the /static path prefix.

Managing static files (e.g. images, JavaScript, CSS), ExpressJS - Serving static files - Static files are files that clients download as they Note that the root route is now set to your public dir, so all static files you load  Root Directory and Index Files; Trying Several Options; Optimizing Performance for Serving Content; Root Directory and Index Files. The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive.

Comments
  • Okay, I got this to work by changing it to r'/(favicon\.ico)'. Why did that work? (I copied it from a similar example in the docs.)
  • seems like setting static_path in the app settings has a special case for favicon and robots.txt. from the docs: we will serve /favicon.ico and /robots.txt from the same [static_path] directory
  • @shino It worked because r'/favicon.ico' is a regular expression and you correctly escaped the '.'
  • Well it would have still matched a literal '.'. The reason that worked is that matched regex groups get passed as arguments to methods of a RequestHandler. The premade StaticFileHandler class expects the path of the file it is looking for relative to either the "static_path" setting or the "path" member on the handlers class. The {'path': static_path} get's passed to the handlers initialize method which then sets it on the instance.
  • @shino you need to extract one group with the filename to serve, I guess. Brackets in regular expression do just that. Try to remove it, and it will tell you TypeError: get() missing 1 required positional argument: 'path', but I didn't test it.