express-unpkg is an express server that serves files from npm packages.
Using npm:
$ npm install --save express-unpkg
Then, use as you would anything else:
// using ES modules
import { createServer } from 'express-unpkg'
// using CommonJS modules
var createServer = require('express-unpkg').createServer
Use createServer
to create a server instance, passing it the options it needs to connect to npm:
import { createServer } from 'express-unpkg'
const server = createServer({
registryURL: 'https://registry.npmjs.org', // The URL of the npm registry, defaults to the public registry
bowerBundle: '/bower.zip', // A special pathname for generating Bower bundles, defaults to "/bower.zip"
redirectTTL: 60, // The time (in seconds) to cache 302 responses, defaults to 0
autoIndex: true // Set false to disable generating index pages for directories
})
server.listen(8080)
server
is a standard node HTTP server.
If you'd like to use express-unpkg as part of a larger express site, you can use the createRequestHandler
function directly. As its name suggests, this function returns another function that can be used as the request handler in a standard express server. This function accepts the same options as createServer
.
import express from 'express'
import { createRequestHandler } from 'express-unpkg'
const app = express()
app.use(express.static('public'))
app.use(createRequestHandler())
// ...
In express-unpkg, the URL is the API. The server recognizes URLs in the format /package@version/path/to/file
where:
package The @scope/name of an npm package (scope is optional)
version The version, version range, or tag
/path/to/file The path to a file in that package (optional, defaults to main module)
To enable debug console output, set DEBUG=express-unpkg
in your environment.