How can I serve static files from a Backend Application Contribution?

I would like to serve static files with extension, but the following doesn’t seem to work:

import { injectable } from 'inversify';
import { BackendApplicationContribution } from '@theia/core/lib/node';
import * as express from 'express';
import path = require('path');

@injectable()
export class TheiaStaticWebBackendApplicationContribution implements BackendApplicationContribution {
    configure(app: express.Application): void {
        const staticPath = path.join(__dirname, '..', '..', 'static');
        app.use('/my-extension/static', express.static(staticPath));
    }
}

I tried adding app.get('/test-response'), and that seems to work fine. Is there a reason app.use() doesn’t work?

PS: the grammar checker on this forum is a bit too strict. It would not allow “BackendApplicationContribution” in the title?

Does anyone know if this is possible?

Have you tried doing it without path parameter in app.use?
app.use(function(…

And check the path inside the function given.

Also see in examples/browser/src-gen/backend/server.js:

application.use(express.static(path.join(__dirname, '../../lib')));
application.use(express.static(path.join(__dirname, '../../lib/index.html')));

Here is the generator code.

@AlexGue If I did app.use() without the path parameter, how would I map the url path? What URL would the files be available at?

@kittaakos If I don’t specify the path, what path will the files be available under?

Under all paths. app.use receives a function middleware where all the requests go through this middleware.
You only have to check for req.path paramenter inside your function to check that the current request is for the path you want to serve the files.

@AlexGue I’m not sure how to do that with express.static?