{"id":17939,"library":"sand-http","title":"Sand HTTP Application Server","description":"sand-http is an HTTP application server module designed for the Sand.js framework. It provides a structured approach to building web applications with features like automatic server lifecycle management, regular expression-based routing, Express.js-style middleware, MVC patterns for controllers and views, and graceful error handling per request. As of version 2.10.2, released in early 2021, the package appears to be in an abandoned or minimal maintenance state, with no significant updates in recent years. It heavily leverages CommonJS module syntax and relies on static Generator Functions for controller actions, a pattern that has largely been superseded by async/await in modern Node.js development. Its unique selling points, such as the `sand.ctx` execution context for helper classes, aim to simplify request-scoped data access within the Sand.js ecosystem. The project emphasizes compatibility with ES6 syntactic features within application code, though the module itself is CommonJS.","status":"abandoned","version":"2.10.2","language":"javascript","source_language":"en","source_url":"https://github.com/SandJS/http","tags":["javascript","http","sand"],"install":[{"cmd":"npm install sand-http","lang":"bash","label":"npm"},{"cmd":"yarn add sand-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add sand-http","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core framework that sand-http extends and integrates with for application lifecycle management and context handling.","package":"sand"},{"reason":"Used for advanced regular expression-based routing in `config/routes.js`.","package":"xregexp"}],"imports":[{"note":"sand-http is a CommonJS module. Direct ESM `import` statements will fail unless transpiled or using Node.js's experimental `--experimental-json-modules` flag with a `type: 'module'` in `package.json`, which is not the intended usage for this library.","wrong":"import Http from 'sand-http';","symbol":"Http","correct":"const Http = require('sand-http');"},{"note":"The `Controller` class is exposed as a named property on the main `sand-http` export, accessible via CommonJS `require` syntax. ESM named imports are not supported without bundling or transpilation.","wrong":"import { Controller } from 'sand-http';","symbol":"Controller","correct":"const Controller = require('sand-http').Controller;"},{"note":"The core `sand` framework is also a CommonJS module and must be imported using `require`.","wrong":"import Sand from 'sand';","symbol":"Sand","correct":"const Sand = require('sand');"}],"quickstart":{"code":"\"use strict\";\n\nconst Http = require('sand-http');\nconst Sand = require('sand');\nconst path = require('path');\nconst fs = require('fs');\n\n// Create dummy config and controller files for quickstart\nconst configDir = path.join(__dirname, 'config');\nconst controllersDir = path.join(__dirname, 'controllers');\nconst viewsDir = path.join(__dirname, 'views');\n\nif (!fs.existsSync(configDir)) fs.mkdirSync(configDir);\nif (!fs.existsSync(controllersDir)) fs.mkdirSync(controllersDir);\nif (!fs.existsSync(viewsDir)) fs.mkdirSync(viewsDir);\n\nfs.writeFileSync(path.join(configDir, 'routes.js'), `\nmodule.exports = {\n\t'/': 'MyController.index'\n};\n`);\n\nfs.writeFileSync(path.join(configDir, 'http.js'), `\nmodule.exports = {\n\tall: {\n\t\tport: 3000\n\t}\n};\n`);\n\nfs.writeFileSync(path.join(controllersDir, 'MyController.js'), `\n\"use strict\";\n\nconst Controller = require('sand-http').Controller;\n\nclass MyController extends Controller {\n\tstatic *index() {\n\t\tthis.res.writeHead(200, {'Content-Type': 'text/plain'});\n\t\tthis.res.end('Hello from Sand HTTP!');\n\t}\n}\n\nmodule.exports = MyController;\n`);\n\nconsole.log('Starting Sand HTTP server on port 3000...');\nlet app = new Sand({log: '*'});\napp.use(Http);\napp.start();\n\nprocess.on('SIGINT', () => {\n  console.log('Stopping Sand HTTP server...');\n  app.shutdown();\n  process.exit();\n});\n","lang":"javascript","description":"This quickstart initializes a Sand.js application with the `sand-http` grain, setting up a basic HTTP server, routes, and a controller. It demonstrates the framework's file structure and the use of static Generator Functions for actions. Access at `http://localhost:3000/`."},"warnings":[{"fix":"Adhere strictly to `static *actionName()` generator function syntax for controller actions. Avoid `async/await` directly in actions unless explicit middleware or context binding is implemented to support it, which is not natively provided by `sand-http` in its current form.","message":"sand-http's controller actions are `static Generator Functions`. Modern Node.js applications predominantly use `async/await` for asynchronous operations. Directly converting these to `async/await` will break the framework's `this.req` and `this.res` context binding, which relies on the generator's `yield` mechanism for request-scoped execution.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"For new projects, consider modern frameworks built with ESM. If using `sand-http`, ensure your project uses CommonJS or a bundler configured to handle CJS modules within an ESM project. Direct `import` statements for `sand-http` will fail at runtime.","message":"The package uses CommonJS (`require`) module syntax exclusively. While Node.js still supports CommonJS, the ecosystem is moving towards ECMAScript Modules (`import/export`). Integrating `sand-http` into an ESM-first project requires transpilation or careful interoperability configurations.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Always double-escape backslashes in regex character classes within route definitions: `'/users/(?<userId>\\\\d+)'` instead of `'/users/(?<userId>\\d+)'`.","message":"Routing patterns in `config/routes.js` use XRegExp-compatible strings, and shortcut character classes like `\\d`, `\\w` must be double-escaped (e.g., `\\\\d`). Failing to double-escape will result in incorrect routing or regex parsing errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"When developing helper functions that rely on `sand.ctx`, ensure they are only called within the scope of a `static Generator Function` action or explicitly pass `this` (the context object) to the helper if it performs asynchronous operations that might lose the `sand.ctx` reference.","message":"The `sand.ctx` global variable is used to access the request-specific context (including `req` and `res`) from helper classes. Relying on global context in asynchronous code can lead to race conditions or incorrect context if not carefully managed, especially with older Node.js versions or non-generator async patterns.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Exercise caution when using this package in new projects or critical applications. Be prepared to fork and maintain the code yourself or migrate to a more actively maintained framework. Evaluate the long-term viability and security implications before committing to `sand-http`.","message":"The `sand-http` package, and its parent framework Sand.js, appear to be abandoned with the last significant update over three years ago. This implies no active maintenance, security patches, or support for newer Node.js features and best practices.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure you are using a Node.js version that supports generator functions (Node.js 6+). If using a transpiler (like Babel), ensure it's configured to handle ES6+ features, including generators.","cause":"Attempting to run `static *generatorFunction()` in an environment that does not support generator functions, or a transpiler is not correctly configured.","error":"SyntaxError: Unexpected token '*'"},{"fix":"Verify that `app` is initialized with `new Sand()` and that `Http` is correctly `require`d from `sand-http`. Ensure the `sand` package itself is correctly installed and initialized.","cause":"The `app` instance is not a `Sand` application or `Http` is not a valid grain.","error":"TypeError: app.use is not a function"},{"fix":"Run `npm install sand-http` in your project directory. Check your `node_modules` folder and ensure `sand-http` is present.","cause":"The `sand-http` package is not installed or not resolvable from the current working directory.","error":"Error: Cannot find module 'sand-http'"},{"fix":"Change `import Http from 'sand-http';` to `const Http = require('sand-http');` and similarly for `Controller` and `Sand`. The package primarily supports CommonJS.","cause":"Attempting to use ES Modules `import` syntax (`import Http from 'sand-http'`) with `sand-http`, which is a CommonJS module, in a file not designated as an ES module.","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}