lws-mime: Local Web Server MIME Type Middleware

2.0.0 · maintenance · verified Wed Apr 22

lws-mime is a middleware designed for the `lws` (local-web-server) ecosystem, enabling developers to customize and override default HTTP response MIME types. Currently stable at version 2.0.0, released in 2017, this package is in maintenance mode, providing a robust and tested solution without frequent updates, but relies on the actively developed `lws` core. Its primary differentiator is deep integration with `lws`, offering programmatic control over how file extensions map to MIME types, thereby ensuring web assets are served with the correct `Content-Type` headers. This is crucial for proper browser rendering and interpretation, especially for custom file formats or when standard MIME sniffing is insufficient. While other general `mime` libraries exist, lws-mime offers a tailored solution for `lws` deployments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to start an `lws` server, integrate `lws-mime` middleware, and define a custom MIME type for a specific file extension.

const lws = require('lws');
const path = require('path');
const fs = require('fs');

// Create a dummy file with a custom extension
const filePath = path.join(__dirname, 'example.custom');
fs.writeFileSync(filePath, '{"message": "Hello, custom world!"}', 'utf8');

const server = lws.create({
  stack: [
    // lws-mime middleware to define custom MIME types
    { module: require('lws-mime'), options: { 'custom': 'application/vnd.my-custom-format+json' } },
    // lws-static to serve the file
    { module: require('lws-static'), options: { root: __dirname } }
  ],
  port: 8000
});

server.listen(() => {
  console.log('Server running on http://localhost:8000');
  console.log('Try accessing: http://localhost:8000/example.custom');
  console.log('The response should have Content-Type: application/vnd.my-custom-format+json');
});

// Clean up dummy file on process exit
process.on('exit', () => {
  if (fs.existsSync(filePath)) {
    fs.unlinkSync(filePath);
  }
});

view raw JSON →