server.js Node.js Framework
server.js is a Node.js framework designed for building web applications and APIs with a focus on simplicity and ease of use. It provides a comprehensive set of features out-of-the-box, including body and file parsers, cookies, sessions, WebSockets (via Socket.io), Redis integration, gzip compression, favicon handling, CSRF protection, and SSL support. This approach aims to minimize configuration and allow developers to concentrate on business logic. The package is currently stable at version 1.0.42, with frequent minor updates addressing bug fixes and introducing small enhancements, as seen in its recent release history. It differentiates itself by bundling many common utilities that often require separate middleware in other frameworks, while still offering compatibility with the Express middleware ecosystem for extended functionality. It explicitly recommends Node.js 8.x.y LTS or newer (minimum 7.6.0), indicating a CommonJS-first approach in its current major version.
Common errors
-
TypeError: ctx.status(...).send is not a function
cause Attempting to send an empty body by just setting the status code, which was a behavior change in version 1.0.12.fixExplicitly call `.send()` after `.status(NUMBER)`, e.g., `ctx.status(200).send()`. -
Error: No response from the server
cause This error or similar warnings (like 'No response was sent from the server') can occur if a route handler does not explicitly return a value or call a response method (e.g., `ctx.send()`, `ctx.render()`, `ctx.redirect()`). Improved error handling for this was added in 1.0.9 and 1.0.13.fixEnsure all route handlers either return a value (which `server` then sends as the response) or explicitly call a response method on the `ctx` object. -
Inconsistent routing with '*' wildcard using Yarn
cause Prior to version 1.0.7, there were known issues where wildcard routes (e.g., `get('*', ...)`) behaved differently when `server` was installed via Yarn compared to npm due to `path-to-regexp-wrap` resolution.fixUpgrade `server` to version 1.0.7 or higher. If upgrading is not an option, consider using npm instead of Yarn, or avoid wildcard routes in affected versions.
Warnings
- gotcha The package officially supports Node.js versions >=7.6.0 and recommends 8.x.y LTS. While it might run on newer Node.js versions, compatibility beyond Node 8.x LTS is not explicitly guaranteed in the documentation, which could lead to unexpected behavior.
- breaking As of version 1.0.12, sending only a status code no longer automatically sets a default body (which was previously handled by Express). To send an empty body with a status, you must explicitly call `.send()` after `.status(NUMBER)`.
- gotcha The package primarily uses CommonJS `require()` syntax in all documentation and examples. While Node.js supports ESM, `server` does not explicitly document or guarantee full ESM compatibility. Attempting to use `import` statements might lead to module resolution errors or unexpected behavior.
- gotcha Prior to version 1.0.7, there were inconsistent path resolution issues when using Yarn (compared to npm) with wildcard routes, specifically affecting `path-to-regexp-wrap` behavior.
Install
-
npm install server -
yarn add server -
pnpm add server
Imports
- server
const server = require('server'); - get, post
import { get, post } from 'server/router';const { get, post } = require('server').router; - render, redirect
import { render, redirect } from 'server/reply';const { render, redirect } = require('server').reply;
Quickstart
const server = require('server');
const { get, post } = server.router;
// Launch server with options and a couple of routes
server({ port: 8080 }, [
get('/', ctx => 'Hello world'),
post('/', ctx => {
console.log(ctx.data);
return 'ok';
})
]);