How to use serve-handler - 10 common examples

To help you get started, we’ve selected a few serve-handler examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github badgen / badgen.net / index.ts View on Github external
const server = http.createServer(async (req, res) => {
  const url = req.url || '/'

  // handle statics
  if (isStatic(url)) {
    return serveHandler(req, res, {
      public: path.resolve(__dirname, PUB_DIR),
      headers: serveStaticHeaders
    })
  }

  // redirects `/docs/:name` to `/:name`
  if (url.startsWith('/docs/')) {
    return sendRedirection(res, 301, url.replace('/docs', ''))
  }

  // handle endpoints
  const handlerName = badgeNames.find(h => matchRoute(`/${h}/:path*`, url))

  try {
    if (handlerName) {
      const handlerPath = path.join(__dirname, 'api', handlerName)
github microsoft / BotFramework-WebChat / __tests__ / setup / setupTestFramework.js View on Github external
const httpServer = createServer((req, res) =>
        handler(req, res, {
          redirects: [
            { source: '/', destination: '__tests__/setup/web/index.html' },
            {
              source: '/createProduceConsumeBroker.js',
              destination: '__tests__/setup/web/createProduceConsumeBroker.js'
            },
            { source: '/mockWebSpeech.js', destination: '__tests__/setup/web/mockWebSpeech.js' }
          ],
          rewrites: [
            { source: '/webchat.js', destination: 'packages/bundle/dist/webchat.js' },
            { source: '/webchat-es5.js', destination: 'packages/bundle/dist/webchat-es5.js' },
            { source: '/webchat-instrumented.js', destination: 'packages/bundle/dist/webchat-instrumented.js' },
            { source: '/webchat-instrumented-es5.js', destination: 'packages/bundle/dist/webchat-instrumented-es5.js' },
            {
              source: '/webchat-instrumented-minimal.js',
              destination: 'packages/bundle/dist/webchat-instrumented-minimal.js'
github microsoft / botframework-solutions / solutions / testharnesses / typescript / assistant-WebChat / packages / server / src / index.js View on Github external
server.get('/**/*', (req, res) => {
    serveHandler(req, res, {
      public: join(__dirname, '../_site')
    });
  });
github stream-labs / streamlabs-obs / app / services / platform-apps / dev-server.ts View on Github external
this.server = http.createServer((request, response) =>
      handler(request, response, {
        public: this.directory,
        cleanUrls: false,
        headers: [
          {
            source: '**',
            headers: [
              {
                key: 'Cache-Control',
                value: 'no-cache, no-store, must-revalidate',
              },
            ],
          },
        ],
      }),
    );
github zeit / now / src / util / dev / server.ts View on Github external
function serveStaticFile(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  cwd: string,
  opts?: object
) {
  return serveHandler(req, res, {
    public: cwd,
    cleanUrls: false,
    etag: true,
    ...opts
  });
}
github compulim / BotFramework-MockBot / src / index.ts View on Github external
server.get('/public/*', async (req, res) => {
  if ('slow' in req.query) {
    res.noCache();

    const release = await acquireSlowQueue();

    await delay(1000);
    release();
  }

  await serveHandler(req, res, {
    path: join(__dirname, './public')
  });
});
github zeit / now / packages / now-cli / src / util / dev / server.ts View on Github external
function serveStaticFile(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  cwd: string,
  opts?: object
) {
  return serveHandler(req, res, {
    public: cwd,
    cleanUrls: false,
    etag: true,
    ...opts,
  });
}
github Gioni06 / stasis-generator / src / lib / serve.ts View on Github external
const server = http.createServer((request: any, response: any) => {
    return handler(request, response, {
      cleanUrls: true,
      trailingSlash: false,
      public: options.public
    });
  });
github zeit / now / src / util / dev / server.ts View on Github external
base
        };
      });

    if (files.length === 0) {
      return false;
    }

    const directory = `/${prefix}`;
    const paths = [
      {
        name: directory,
        url: requestPath
      }
    ];
    const directoryHtml = directoryTemplate({
      files,
      paths,
      directory
    });
    this.setResponseHeaders(res, nowRequestId);
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    res.setHeader(
      'Content-Length',
      String(Buffer.byteLength(directoryHtml, 'utf8'))
    );
    res.end(directoryHtml);
    return true;
  }
github zeit / now / packages / now-cli / src / util / dev / server.ts View on Github external
base,
        };
      });

    if (files.length === 0) {
      return false;
    }

    const directory = `/${prefix}`;
    const paths = [
      {
        name: directory,
        url: requestPath,
      },
    ];
    const directoryHtml = directoryTemplate({
      files,
      paths,
      directory,
    });
    this.setResponseHeaders(res, nowRequestId);
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    res.setHeader(
      'Content-Length',
      String(Buffer.byteLength(directoryHtml, 'utf8'))
    );
    res.end(directoryHtml);
    return true;
  }

serve-handler

The routing foundation of `serve`

MIT
Latest version published 19 days ago

Package Health Score

87 / 100
Full package analysis

Popular serve-handler functions