express-random
v1.0.2
Published
Express middleware to generate entropy via random bytes from Node.js crypto module.
Maintainers
Readme
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-randomExample
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
| Name | Type | Description |
|------|------|-------------|
| req | import('express').Request | The HTTP request. |
| res | import('express').Response | The HTTP response. |
Returns
| Type | Description |
|------|-------------|
| void | Sends 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-storeto 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.
