session-handler-server
v1.0.1
Published
A simple utility module that provides Express setup and utility functions
Maintainers
Readme
Session Handler Server
A robust utility module that provides session management with flow control capabilities for Node.js applications.
Installation
npm install session-handler-serverUsage
// Import the module
import { SessionManager, SessionHandler } from 'session-handler-server';
// Define your flow rules
const flowRule = {
entry: { actionName: 'start' },
actions: {
start: {
flowTo: async ({ sessionClient, sessionId }) => {
// Logic to determine next action
return { actionName: 'nextStep' };
}
},
// Define other actions...
}
};
// Create a session manager instance
const sessionManager = new SessionManager({
mongoURI: 'your-mongodb-connection-string',
flowRule
});
// Use session routes in your Express app
const express = require('express');
const app = express();
app.use('/api/sessions', SessionHandler);
// Start your server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});Features
- Session management with MongoDB persistence
- Flow control for defining multi-step processes
- API endpoints for session operations
- Activity tracking within sessions
- Error handling and response formatting
API Reference
SessionManager
Core class for handling session operations:
fetchSessionById(sessionId): Retrieve session by IDaddSession(): Create a new sessionaddSessionActivity(sessionId, activity): Add activity to a sessionupdateSessionActivity(sessionId, activityId, modifications): Update session activityfetchLastSessionActivity(sessionId): Get the most recent activity in a sessiongetSessionSummary(sessionId): Retrieve summary of session activities
FlowController
Extends SessionManager to provide flow control capabilities:
initiateSession(): Start a new session flowcontinueSession(sessionId): Move to the next action in the flowgetNextAction(actionName, sessionId): Determine the next action based on flow rules
License
MIT
