Registry / testing / jsend
library1.1.0jsnpmunverified

Utilities and middleware for creating, validating, and handling jsend JSON response objects in Node.js applications. Version 1.1.0 is the latest stable release. Provides functions to construct success/fail/error responses, validate them (including strict mode), convert from Node-style callbacks, and Express middleware to attach res.jsend helpers. Key differentiator: fully implements the jsend spec with built-in validation and middleware, unlike ad-hoc implementations.

npm install jsend
INSTALL
IMPORT
SIG · JSEND
J
jsend
testingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
import jsend from 'jsend'
import { jsend } from 'jsend'
The package exports a single function (the jsend object) as default. Named import will not work.
require
const jsend = require('jsend')
CommonJS require works fine. The returned value is the jsend object.
strict mode
import jsend from 'jsend'; const strictJsend = jsend({ strict: true })
import jsend from 'jsend'; jsend.strict = true
Strict mode is configured by calling the default export as a function with an options object. It returns a new jsend instance.

Demonstrates basic usage: creating success/fail/error responses, validating, and using the Express middleware.

import jsend from 'jsend'; // Create a success response const successRes = jsend.success({ foo: 'bar' }); console.log(successRes); // { status: 'success', data: { foo: 'bar' } } // Create a fail response const failRes = jsend.fail({ validation: 'missing field' }); console.log(failRes); // { status: 'fail', data: { validation: 'missing field' } } // Create an error response const errorRes = jsend.error('Something went wrong'); console.log(errorRes); // { status: 'error', message: 'Something went wrong' } // Validate a response console.log(jsend.isValid(successRes)); // true // Express middleware example import express from 'express'; const app = express(); app.use(jsend.middleware); app.get('/data', (req, res) => { res.jsend.success({ id: 1 }); }); app.listen(3000);
Debug
Known issues
gotchaWhen using strict validation via jsend({ strict: true }), the returned object is a new instance with its own methods. Mixing strict and non-strict instances will not work as expected.
fix
Use the same instance consistently or re-create strict version.
affects: >=0.0.0
gotchaThe middleware attaches res.jsend which can be used as a callback (res.jsend(err, data)). However, if err is truthy, it will call res.jsend.fail(data) but the spec expects res.jsend.error(message) for errors. This may cause confusion.
fix
Manually check error and call res.jsend.error() instead.
affects: >=0.0.0
gotchajsend.forward does not handle errors correctly for 'fail' status; it treats 'fail' as an error (setting the callback's first argument) which may not be expected.
fix
Use jsend.fromArguments or manually parse the response.
affects: >=0.0.0
breakingNo breaking changes known; version 1.1.0 is stable and backward-compatible with 1.x.
fix
None needed.
affects: >=1.0.0 <=1.1.0
Errors
Common errors & fixes
TypeError: jsend.isValid is not a function
Using named import instead of default import.
fix
Use 'import jsend from "jsend"' or 'const jsend = require("jsend")'
res.jsend is not a function
Middleware not applied to the Express app before route definition.
fix
Ensure app.use(jsend.middleware) is called before defining routes.
ValidationError: Unexpected property 'junk'
Strict mode is enabled and the response contains extra properties.
fix
Disable strict mode or remove extra properties from the response.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
packagejsend
jsend — npm install jsend · libregistry