Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serve-handler

Package Overview
Dependencies
Maintainers
8
Versions
66
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`

  • 6.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9M
increased by11.3%
Maintainers
8
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 15 Oct 2024

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