Node.js Backend Examples for LARC/PAN
This directory contains Node.js versions of the PHP backend examples, making it easier to get started with LARC without requiring PHP.
Available Backends
1. Legacy API (api-legacy.js)
Simple database API with basic CRUD operations. No authentication required.
Port: 3000
Endpoints:
GET /?x=list_resources- List available database tablesGET /?x=get&rsc=tablename- Get records from a table (with pagination)GET /?x=list_fields&rsc=tablename- Get table schema
2. Secure API (apps/api.js)
Production-ready API with security features.
Port: 3001
Features:
- Session-based authentication
- CSRF protection
- Rate limiting
- Resource whitelisting
- SQL injection protection
- Security headers
3. Authentication (auth.js)
Handles user authentication with JWT tokens.
Port: 3002
Endpoints:
GET /?action=login- User login (returns JWT)GET /?action=logout- User logoutGET /?action=refresh- Refresh JWT tokenGET /?action=check- Check authentication statusGET /?action=csrf- Get CSRF token
- Email:
demo@example.com - Password:
demo123
4. SSE Server (sse.js)
Server-Sent Events hub for real-time PAN messaging.
Port: 3003
Endpoints:
GET /?topics=topic1,topic2&lastEventId=123- Subscribe to event streamPOST /with{"topic": "demo.ping", "data": {...}, "retain": false}- Publish event
Installation
# Install dependencies
npm install
# Or with yarn
yarn install
Usage
Run Individual Servers
# Legacy API
npm run api-legacy
# Secure API
npm run api
# Authentication
npm run auth
# SSE Server
npm run sse
Run All Servers Concurrently
npm run backends
This will start all four servers on their respective ports:
- Legacy API: http://localhost:3000
- Secure API: http://localhost:3001
- Auth: http://localhost:3002
- SSE: http://localhost:3003
Configuration
Create a .env file in this directory with your database settings:
[db]
type=mysql
host=localhost
user=root
pass=
db=test
# For SQLite (optional)
# type=sqlite
# file=pan_demo.db
[demo]
mode=true
[security]
jwt_secret=change-this-secret-in-production
Database Support
Both MySQL and SQLite are supported:
- MySQL: Use the
mysql2package (already included) - SQLite: Use the
better-sqlite3package (already included)
.env file.
Security Notes
Development vs Production
The examples are configured for development use. For production:
.envdemo.mode=false).env filePassword Hashing
The authentication server uses bcryptjs for password hashing. The demo password is pre-hashed for convenience.
Comparison with PHP Versions
| Feature | PHP Version | Node.js Version | |---------|-------------|-----------------| | No build step | ✓ | ✓ | | MySQL support | ✓ | ✓ | | SQLite support | ✓ (PDO) | ✓ (better-sqlite3) | | Sessions | ✓ (built-in) | ✓ (in-memory Map) | | CSRF protection | ✓ | ✓ | | Rate limiting | ✓ | ✓ | | JWT support | ✓ (manual) | ✓ (crypto) | | SSE | ✓ (file-based) | ✓ (file-based + in-memory) |
Examples
Fetch Resources
// Get list of available resources
const response = await fetch('http://localhost:3000/?x=list_resources');
const data = await response.json();
console.log(data.Resources);
Get Data with Pagination
// Get first 10 products
const response = await fetch('http://localhost:3000/?x=get&rsc=products&page_size=10&start=0');
const data = await response.json();
console.log(data.results);
Authenticate
// Login
const response = await fetch('http://localhost:3002/?action=login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'demo@example.com',
password: 'demo123'
})
});
const data = await response.json();
console.log(data.token);
Subscribe to SSE
const events = new EventSource('http://localhost:3003/?topics=demo.*');
events.addEventListener('demo.ping', (e) => {
const data = JSON.parse(e.data);
console.log('Received:', data);
});
Publish SSE Event
await fetch('http://localhost:3003/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
topic: 'demo.ping',
data: { message: 'Hello from Node.js!' },
retain: false
})
});
Troubleshooting
Port Already in Use
If you get an "EADDRINUSE" error, another process is using the port. Either:
node api-legacy.js 3010Database Connection Errors
- Check your
.envconfiguration - Ensure MySQL is running (if using MySQL)
- Verify database credentials
- Create the database if it doesn't exist
Module Not Found
Run npm install to install all dependencies.
License
MIT © LARC Contributors