Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

upgradeExpress

Upgrade Express vals to the Web API without breaking existing consumers

How it works

upgradeExpress wraps a Web handler into a function that works as normal when called by the Web API, and redirects to the Web API when called by the Express API.

Example

I don't have an example for this per se but I used it to upgrade @easrng/button.

1
2
3
4
5
6
7
8
9
10
11
12
13
export const upgradeExpress = (handler: (request: Request) => Response | PromiseLike<Response>) =>
function(request: Request | express.Request, response?: express.Response): Response | Promise<Response> {
if (request instanceof Request) {
const response = handler(request);
if (response instanceof Response) {
return response;
} else {
return Promise.resolve(response);
}
}
const domain: string = request.get("host").replace(/\.express\.val\.run$/, ".web.val.run");
response!.redirect(`https://${domain}${request.originalUrl}`);
};
March 12, 2024