The plan:
- deploy to Now.sh with serverless
- use Now.sh CDN cache
First let's make sure your project is serverless.
Presuming your code is in index.js
, instead of ending your code with addon.serveHTTP
, use:
module.exports = addon.getInterface()
where addon
is:
const { addonBuilder } = require("stremio-addon-sdk")
const addon = new addonBuilder(manifest);
Create serverless.js
, which includes:
const { getRouter } = require("stremio-addon-sdk");
const addonInterface = require("./addon");
const router = getRouter(addonInterface);
module.exports = function(req, res) {
router(req, res, function() {
res.statusCode = 404;
res.end();
});
}
Create a now.json
file that includes:
{
"version": 2,
"builds": [
{ "src": "serverless.js", "use": "@now/node" }
],
"routes": [
{ "src": "/.*", "dest": "/serverless.js" }
]
}
Now go to now.sh and create an account if you don't have one, then install the now
cli tool with:
npm install -g now
Open a terminal window, go to your project's directory and simply write now
, this will prompt for login, after you login you'll get your Now.sh URL.
It is important to set the cacheMaxAge
parameter in the responses with the number of seconds you want the responses cached by Now.sh's CDN. Caching is important to reduce the number of requests and ensuring the longevity of your addon.
The cacheMaxAge
parameter is documented for all request handlers.
You're done!