Socket
Socket
Sign inDemoInstall

serve-handler

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serve-handler

The routing foundation of `serve` and static deployments on Now


Version published
Weekly downloads
1.7M
increased by2.98%
Maintainers
1
Weekly downloads
 
Created

What is serve-handler?

The serve-handler package is designed to be used with a Node.js server to serve static files, single-page applications, and directory listings. It provides a simple way to configure how files are served, with options for rewrites, redirects, headers, and more.

What are serve-handler's main functionalities?

Serving static files

This code creates a simple server that serves static files from the current directory on port 3000 using the default configuration.

const serveHandler = require('serve-handler');
const http = require('http');

http.createServer((request, response) => {
  return serveHandler(request, response);
}).listen(3000);

Custom routing with rewrites

This code demonstrates how to use the rewrites option to redirect requests from 'some/path/*' to '/index.html', which is useful for single-page applications.

const serveHandler = require('serve-handler');
const http = require('http');

http.createServer((request, response) => {
  return serveHandler(request, response, {
    rewrites: [{ source: 'some/path/*', destination: '/index.html' }]
  });
}).listen(3000);

Custom headers

This code shows how to set custom headers for all files served. In this example, all responses will include the 'X-Custom-Header' with the value 'Custom Value'.

const serveHandler = require('serve-handler');
const http = require('http');

http.createServer((request, response) => {
  return serveHandler(request, response, {
    headers: [{ source: '**/*', headers: [{ key: 'X-Custom-Header', value: 'Custom Value' }] }]
  });
}).listen(3000);

Directory listings

This code enables directory listings, allowing users to view the contents of directories that do not contain an index file.

const serveHandler = require('serve-handler');
const http = require('http');

http.createServer((request, response) => {
  return serveHandler(request, response, {
    directoryListing: true
  });
}).listen(3000);

Other packages similar to serve-handler

Keywords

FAQs

Package last updated on 21 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc