importexpressfrom'express';importwkjfrom'well-known-json';constapp=express();// Each resource will be a separate JSON resource under the well-known endpointconstresources={'foo/bar': {// Will be at .well-known/foo/bara: 1,b: 2,},'baz': {// Will be at .well-known/bazobj: {// Function properties are evaluated for each response generatednow: function(){returnDate.now();},// String properties that look like relative URIs are converted to absolute URIsuri: './relative/path',},// Other things become JSON normallystr: 'words here',},};// Options for well-known-json middleware (and middlewares it uses)constoptions={// Passed directly to cors middlewarecors: {/* whatever you can give the cors middleware */},// Optional base for resolving relative URIs// If omitted, wkj will use the protocol and host to which the request was sentbaseUri: 'http://example.org/foo',};// Create a middleware instanceconstwkjMiddleware=wkj(resources,options);// Mount the middleware with expressapp.use(wkjMiddleware);// Add additional resources after creation// They will be merged with a prexisting resource with the same namewkjMiddleware.addResource('baz',{more: 'stuff',// Add key more to bazstr: 'different words here',// Overwrite key str in baz from before});
Features
Enables CORS (including pre-flight) for its corresponding JSON documents
Converts relative URIs to absolute
Can have functions "in the documents" which get evaluated for each request
Supports de-facto standard for reverse proxies (i.e. X-Forwarded-* headers)