API Reference
Complete reference for NetFUNNEL Node.js Agent functions and usage.
Netfunnel Class
The core class of the Node.js Agent. Used in Express or Nuxt middleware to perform NetFUNNEL queue control.
initialize Method
Purpose: Initializes the NetFUNNEL agent.
Function Signature:
Netfunnel.initialize(config: NetfunnelConfig): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
config | NetfunnelConfig | NetFUNNEL initialization configuration object |
Example:
import { Netfunnel } from 'netfunnel-node-agent';
Netfunnel.initialize({
clientId: '{{CLIENT_ID}}',
secretKey: '{{SECRET_KEY}}'
});
Note: Initialization should be performed only once when the server starts. For Express, perform it in app.ts, and for Nuxt, perform it in middleware.
run Method
Purpose: Executes NetFUNNEL queue control.
Function Signature:
Netfunnel.run(req: IncomingMessage, res: ServerResponse): Promise<boolean>
Parameters:
| Parameter | Type | Description |
|---|---|---|
req | IncomingMessage | Node.js HTTP request object |
res | ServerResponse | Node.js HTTP response object |
Return Value: Promise<boolean>
true: Request is processed normally and can proceed to the next middlewarefalse: Request is blocked or redirected, so it should not proceed to the next middleware
Behavior:
- Trigger rule matching: Compare the URL of the page accessed by the user with trigger rules
- When rules match: Request entry to NetFUNNEL server
- When waiting response: Automatically redirect to waiting room page (302)
- When entry succeeds: Issue key and continue with the request
Example:
// Used in Express middleware
import { Netfunnel } from 'netfunnel-node-agent';
export function netfunnelMiddleware(req, res, next) {
Netfunnel.run(req, res).then(allowed => {
if (!allowed) {
return res.redirect(302, req._nfRedirect || '/');
}
next();
});
}
// Used in Nuxt middleware
import { Netfunnel } from 'netfunnel-node-agent';
import { sendRedirect } from 'h3';
export default defineEventHandler(async event => {
const allowed = await Netfunnel.run(event.node.req, event.node.res);
if (!allowed) {
const redirectUrl = (event.node.req as any)._nfRedirect || '/';
return sendRedirect(event, redirectUrl);
}
});
getVersion Method
Purpose: Gets the NetFUNNEL agent version.
Function Signature:
Netfunnel.getVersion(): string
Return Value: string - Agent version string
Example:
import { Netfunnel } from 'netfunnel-node-agent';
console.log('NetFUNNEL Version:', Netfunnel.getVersion());
Operation Flow
Normal Flow
- User accesses page
- Call
Netfunnel.run()in middleware - Check trigger rule matching
- When rules match, request entry to NetFUNNEL server
- When entry is allowed, issue key and continue with request
run()method returnstrue
Waiting Flow
- User accesses page
- Call
Netfunnel.run()in middleware - Check trigger rule matching
- When rules match, request entry to NetFUNNEL server
- Receive waiting response
- Automatically redirect to waiting room page (302)
run()method returnsfalse
Blocked Flow
- User accesses page
- Call
Netfunnel.run()in middleware - Check trigger rule matching
- When rules match, request entry to NetFUNNEL server
- Receive blocked response
- Redirect to blocked page or handle blocking
run()method returnsfalse
Best Practices
Centralized Configuration
// config/netfunnel.ts
import { Netfunnel } from 'netfunnel-node-agent';
export function initializeNetfunnel() {
Netfunnel.initialize({
clientId: process.env.NETFUNNEL_CLIENT_ID!,
secretKey: process.env.NETFUNNEL_SECRET_KEY!
});
}
Environment-Specific Configuration
import { Netfunnel } from 'netfunnel-node-agent';
Netfunnel.initialize({
clientId: process.env.NETFUNNEL_CLIENT_ID!,
secretKey: process.env.NETFUNNEL_SECRET_KEY!,
printLog: process.env.NODE_ENV === 'development'
});
Error Handling
export function netfunnelMiddleware(req, res, next) {
Netfunnel.run(req, res)
.then(allowed => {
if (!allowed) {
return res.redirect(302, req._nfRedirect || '/');
}
next();
})
.catch(error => {
console.error('NetFUNNEL error:', error);
// Continue service even when error occurs
next();
});
}
Related Documentation
- Installation and Initialization: How to apply Express/Nuxt middleware
- Initialization Settings: All initialization parameters
- Troubleshooting: Common issues and solutions