Skip to content

Commit cfbcfd0

Browse files
freeMode added
1 parent 8e795dd commit cfbcfd0

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

docs/Express.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,32 @@ Serve large files with partial content support (`Range` headers), such as video
538538

539539
---
540540

541+
## 🚀 `freeMode(folder)`
542+
543+
A quick way to launch a **test-only server environment** in Express.js for static files and open CORS access.
544+
545+
### 🔧 What it does
546+
547+
- Serves static files from a folder you specify
548+
- Enables **unrestricted CORS** (for all origins, headers, and methods)
549+
- Only works in **development mode** (`NODE_ENV=development`)
550+
551+
### ⚠️ Warning
552+
553+
**Do NOT use this in production!**
554+
555+
This function:
556+
- Disables all security headers
557+
- Exposes everything to the public
558+
- Should only be used for local testing or experiments
559+
560+
### ✅ Usage
561+
562+
```js
563+
app.freeMode('./public');
564+
565+
---
566+
541567
## 🧠 Notes
542568

543569
* The `sendFile()` method is optimized for small/medium binary/text blobs that fit in memory.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tiny-server-essentials",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A good utility toolkit to unify Express v5 and Socket.IO v4 into a seamless development experience with modular helpers, server wrappers, and WebSocket tools.",
55
"scripts": {
66
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",

src/TinyExpress.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class TinyExpress {
230230
},
231231
);
232232
}
233+
233234
/**
234235
* Modifies a CSRF config option, only if the key exists and value is a string.
235236
* @param {string} key - Either "cookieName", "headerName", or "errMessage".
@@ -954,6 +955,47 @@ class TinyExpress {
954955
} else throw new Error('Either "stream" must be provided.');
955956
}
956957
}
958+
959+
/**
960+
* Enables a lightweight test-only Express mode for serving static files and open API testing.
961+
*
962+
* This method sets up an unrestricted CORS environment and exposes a static folder,
963+
* allowing any origin to access the content and APIs freely. It also includes permissive
964+
* CORS headers and custom middleware headers for development diagnostics.
965+
*
966+
* ⚠️ **Do not use this in production!**
967+
* This mode disables all security measures and is intended **only** for local development,
968+
* debugging, or experimentation. All origins, headers, and methods are allowed without validation.
969+
*
970+
* @throws {Error} If the current environment is not "development".
971+
* @throws {TypeError} If `folder` is not a non-empty string.
972+
*
973+
* @param {string} folder - The path to the folder that will be served as static content.
974+
*/
975+
freeMode(folder) {
976+
if (process.env.NODE_ENV !== 'development')
977+
throw new Error('[freeMode] This method is only allowed in development mode.');
978+
if (typeof folder !== 'string' || folder.trim() === '')
979+
throw new TypeError('[freeMode] Expected "folder" to be a non-empty string.');
980+
this.root.use(express.static(folder));
981+
this.root.use(function (req, res, next) {
982+
// Website you wish to allow to connect
983+
res.setHeader('Access-Control-Allow-Origin', '*');
984+
985+
// Request methods you wish to allow
986+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
987+
988+
// Request headers you wish to allow
989+
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
990+
991+
// Set to true if you need the website to include cookies in the requests sent
992+
// to the API (e.g. in case you use sessions)
993+
res.setHeader('Access-Control-Allow-Credentials', 'true');
994+
995+
// Pass to next layer of middleware
996+
next();
997+
});
998+
}
957999
}
9581000

9591001
export default TinyExpress;

0 commit comments

Comments
 (0)