New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

express-random

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-random

Express middleware to generate entropy via random bytes from Node.js crypto module.

latest
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

express-random

Canonical URL:
https://alexstevovich.com/a/express-random-nodejs

Software URL:
https://midnightcitylights.com/software/express-random-nodejs

A Express middleware to generate entropy via random bytes from Node.js crypto module.

Note: This package is a community-developed middleware for the Express framework and is not officially endorsed or maintained by the Express team.

Installation

npm install express-random

Example

import express from 'express';
import randomBytesHandler from 'express-random';

const app = express();

// Set up the /rng endpoint
app.get('/rng', randomBytesHandler);

app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000');
});

ClientJS Post example

// Specify the byte size in the query parameter
const response = await fetch('http://localhost:3000/rng?bytes=64', {
    method: 'GET',
});

if (response.ok) {
    // Convert the response to an ArrayBuffer or Blob and display
    const buffer = await response.arrayBuffer();
    const byteArray = new Uint8Array(buffer);
    document.getElementById('result').textContent = `Random Bytes (64 bytes):\n${byteArray.join(', ')}`;
}

Function

randomBytesHandler(req, res)

Provides a random byte stream in response to an HTTP request.

Parameters

NameTypeDescription
reqimport('express').RequestThe HTTP request.
resimport('express').ResponseThe HTTP response.

Returns

TypeDescription
voidSends a random byte stream as a binary response.

Notes

  • Generates a random byte stream based on the crypto.randomBytes() function.
  • The default size of the byte stream is 32 bytes, but the size can be specified in the query string as ?bytes=SIZE (e.g., /rng?bytes=64).
  • Limits are applied to the size to ensure it's within the range of 1 to 65536 bytes.
  • The function returns an octet-stream with Cache-Control: no-store to ensure it is not cached.
  • Ideal for use cases requiring secure random values, such as cryptography, token generation, and entropy collection.

License

Licensed under the MIT License.

Disclaimer

This package generates random bytes based on Node.js’s built-in crypto.randomBytes(). While this may be suitable for many use cases, users should evaluate its suitability for specific, especially sensitive, applications.

Keywords

express

FAQs

Package last updated on 12 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts