express-protocol-handler
Resolve custom protocols using Express middleware.
Instalation
$ npm install express-protocol-handler
Usage
const app = ;const protocolHandler = ; const port = 3000;protocolHandlerprotocol's3://' 'https://example.com'; app;app; // Standalone usageprotocolHandler;//=> https://example.com
GET /resolve?url=s3://test HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:3000
User-Agent: HTTPie/0.9.9
HTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 41
Content-Type: text/plain; charset=utf-8
Date: Thu, 20 Dec 2018 13:25:26 GMT
Location: https://example.com
Vary: Accept
X-Powered-By: Express
Found. Redirecting to https://example.com
API
Table of Contents
ProtocolHandler
Create protocol handler
Parameters
param
String name of query param containing target url (optional, default'url'
)options
ProtocolHandlerOptions protocol handler options (optional, default{}
)options.blacklist
(optional, default[]
)
protocol
Register protocol handler
Parameters
scheme
String protocol schemehandler
ProtocolCallback protocol handler
Examples
// register multiple handlersconst handler = ;handler protocol's3://' resolve protocol'gdrive://' resolve;
Returns ProtocolHandler instance to allow chaining
protocols
Properties
Examples
// check if protocol is registeredconst handler = ;handlerprotocol's3://' resolve;console;//=> true
resolve
Resolve url with registered protocol handler
Parameters
url
String target url
Examples
// create handlerconst handler = ;handlerprotocol's3://' 'https://example.com';// resolve urlhandler;//=> https://example.com
Returns Promise<String> resolved url, redirect location
middleware
Returns Express middleware
Examples
// create handlerconst handler = ;handlerprotocol's3://' resolve;// attach to express appapp;
Returns function (IRequest, IResponse) Express middleware
module.exports
Create new ProtocolHandler instance
Parameters
param
String name of query param containing target url (optional, default'url'
)options
ProtocolHandlerOptions protocol handler options (optional, default{}
)
Examples
const handler = 'query';
Returns ProtocolHandler instance
ProtocolHandlerOptions
Type: Object
Properties
ProtocolCallback
Resolver function for specific protocol
Type: Function
Parameters
url
String target url
Examples
// Resolve gdrive urlsconst fetchInfo = ; { const itemId = urlpathname; const fileInfo = await ; return fileInfodownloadUrl;}
Returns (String | Promise<String>) resolved url redirect location
IRequest
Express server request
IResponse
Express server response