My Server Minimal HTTP Server
My Server (npm package `my-server`) is a minimalist, zero-dependency HTTP server for Node.js. It allows developers to quickly set up a simple web server to serve files or respond to HTTP requests with custom handlers. Currently stable at version 2.0.7, the package was last published in January 2020, suggesting a low-cadence or maintenance mode of development, with no significant updates since. Its primary differentiator is its extreme simplicity and lack of external dependencies, making it suitable for lightweight use cases like local development, prototyping, or basic API mocking, without the overhead of larger frameworks.
Common errors
-
TypeError: server.listen is not a function
cause The imported `my-server` function was not invoked, so `server` is still a function, not the server instance.fixEnsure the `my-server` function is called: `const server = require('my-server')();` -
Error: listen EADDRINUSE: address already in use :::5000
cause Another process is already using the specified port (e.g., port 5000).fixChange the port number passed to `server.listen(PORT)` or terminate the process currently using the port. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ES Module (`.mjs` file or `"type": "module"` in `package.json`) context.fixThe `my-server` package is CJS-only. To use it in an ESM project, consider a dynamic import (`import('my-server').then(mod => mod() )`) or stick to CommonJS for this part of your application. For new projects, prefer modern ESM-compatible server frameworks.
Warnings
- gotcha The `my-server` package is a CommonJS module (CJS) and does not natively support ES Modules (ESM) `import` syntax. Attempting to `import` it in a pure ESM context (e.g., in a Node.js project with `"type": "module"` set in `package.json`) will likely result in an error or require manual interoperability configuration.
- deprecated The `my-server` package has not been updated since January 2020. While a simple file server may not require frequent updates, this indicates a lack of active maintenance, which could lead to compatibility issues with newer Node.js versions or unaddressed security vulnerabilities for production use cases.
- gotcha The package exports a function that must be *invoked* (`()`) immediately after `require()` to obtain the server instance. Forgetting to call this function will lead to a `TypeError` when attempting to call methods like `listen` or `all` on the raw function.
Install
-
npm install my-server -
yarn add my-server -
pnpm add my-server
Imports
- server
const myServer = require('my-server'); // or import { server } from 'my-server';const server = require('my-server')(); - server.listen
require('my-server').listen(5000);server.listen(5000);
Quickstart
const server = require('my-server')();
// Register a universal handler for all incoming requests
server.all(function (req, res) {
// Log details of the incoming request
console.log(`Received ${req.method} request for ${req.url}`);
// Respond with a JSON object containing request details
res.json({
headers: req.headers,
method: req.method,
range: req.headers.range,
url: req.url
}, 0, 2); // The 0 and 2 are indentation arguments for JSON.stringify
});
// Start the server on port 5000
const port = process.env.PORT ?? 5000;
server.listen(port, () => {
console.log(`My Server is running on http://localhost:${port}`);
console.log('Access http://localhost:5000/some/path to see response.');
});