@awesomeorganization/static-handler

1.4.3 • Public • Published

static-handler

💥 [ESM] The static handler for Node.js according to rfc7230, rfc7231, rfc7232, rfc7233, rfc7234 and whatwg


GitHub Workflow Codacy CodeFactor Snyk Depfu npms.io


Install

npm install @awesomeorganization/static-handler

Example

Full example in /example folder.

const rewriteMiddleware = rewriteHandler({
  rules: [
    {
      pattern: '^/old-files/(.*)',
      replacement: '/files/$1',
      statusCode: REDIRECT_STATUS_CODES.MOVED_PERMANENTLY,
    },
    {
      pattern: '(.*)/$',
      replacement: '$1/index.txt',
    },
  ],
})
const staticMiddleware = await staticHandler({
  directoryPath: './static',
})
http({
  handlers: {
    request(request, response) {
      rewriteMiddleware.handle({
        request,
        response,
      })
      staticMiddleware.handle({
        request,
        response,
      })
    },
  },
  listenOptions: {
    host: '127.0.0.1',
    port: 3000,
  },
})
// TRY
// http://127.0.0.1:3000/
// http://127.0.0.1:3000/files/
// http://127.0.0.1:3000/files/somefile.txt
// http://127.0.0.1:3000/old-files/
// http://127.0.0.1:3000/old-files/somefile.txt

Package Sidebar

Install

npm i @awesomeorganization/static-handler

Weekly Downloads

6

Version

1.4.3

License

MIT

Unpacked Size

18.7 kB

Total Files

6

Last publish

Collaborators

  • sergey.sedoy.97