@robinpath/server
0.1.3Node.jsPublicHTTP server creation and management using Node.js built-in http module. Supports routing, static file serving, CORS, and common response helpers.
Server
HTTP server creation and management using Node.js built-in http module. Supports routing, static file serving, CORS, and common response helpers.
Package: @robinpath/server | Category: Web | Type: Utility
Authentication
No authentication required. All functions are available immediately.
Use Cases
Use the server module when you need to:
- Create a new HTTP server instance -- Use
server.createto perform this operation - Start listening for incoming connections -- Use
server.startto perform this operation - Stop the server and close all connections -- Use
server.stopto perform this operation - Register a handler for all incoming requests -- Use
server.onRequestto perform this operation - Register an error handler for the server -- Use
server.onErrorto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
create | Create a new HTTP server instance | object |
start | Start listening for incoming connections | object |
stop | Stop the server and close all connections | object |
onRequest | Register a handler for all incoming requests | object |
onError | Register an error handler for the server | object |
route | Add a route with method, path pattern, and handler | object |
static | Serve static files from a directory | object |
sendJson | Send a JSON response | object |
sendHtml | Send an HTML response | object |
sendFile | Send a file as the response | object |
sendRedirect | Send an HTTP redirect response | object |
status | Send a response with a specific status code | object |
cors | Enable and configure CORS headers for a server | object |
getServers | List all active server instances | object |
getRoutes | List all routes registered on a server | object |
Functions
create
Create a new HTTP server instance
Module: server | Returns: object -- API response.
server.create
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Unique server identifier |
port | number | No | Port to listen on (default 3000) |
host | string | No | Host to bind to (default 0.0.0.0) |
start
Start listening for incoming connections
Module: server | Returns: object -- API response.
server.start
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
stop
Stop the server and close all connections
Module: server | Returns: object -- API response.
server.stop
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
onRequest
Register a handler for all incoming requests
Module: server | Returns: object -- API response.
server.onRequest
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
handler | string | No | Request handler function (req, res) |
onError
Register an error handler for the server
Module: server | Returns: object -- API response.
server.onError
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
handler | string | No | Error handler function (err) |
route
Add a route with method, path pattern, and handler
Module: server | Returns: object -- API response.
server.route
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
method | string | No | HTTP method (GET, POST, etc.) |
path | string | No | Route path pattern (supports :param and *) |
handler | string | No | Route handler function (req, res) |
static
Serve static files from a directory
Module: server | Returns: object -- API response.
server.static
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
dir | string | No | Directory path to serve files from |
sendJson
Send a JSON response
Module: server | Returns: object -- API response.
server.sendJson
| Parameter | Type | Required | Description |
|---|---|---|---|
res | object | No | HTTP response object |
data | any | No | Data to serialize as JSON |
status | number | No | HTTP status code (default 200) |
sendHtml
Send an HTML response
Module: server | Returns: object -- API response.
server.sendHtml
| Parameter | Type | Required | Description |
|---|---|---|---|
res | object | No | HTTP response object |
html | string | No | HTML content |
status | number | No | HTTP status code (default 200) |
sendFile
Send a file as the response
Module: server | Returns: object -- API response.
server.sendFile
| Parameter | Type | Required | Description |
|---|---|---|---|
res | object | No | HTTP response object |
path | string | No | Path to the file |
status | number | No | HTTP status code (default 200) |
sendRedirect
Send an HTTP redirect response
Module: server | Returns: object -- API response.
server.sendRedirect
| Parameter | Type | Required | Description |
|---|---|---|---|
res | object | No | HTTP response object |
url | string | No | URL to redirect to |
status | number | No | HTTP status code (default 302) |
status
Send a response with a specific status code
Module: server | Returns: object -- API response.
server.status
| Parameter | Type | Required | Description |
|---|---|---|---|
res | object | No | HTTP response object |
code | number | No | HTTP status code |
body | string | No | Response body text |
cors
Enable and configure CORS headers for a server
Module: server | Returns: object -- API response.
server.cors
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
origin | string | No | Allowed origin (default *) |
methods | string | No | Allowed methods |
headers | string | No | Allowed headers |
getServers
List all active server instances
Module: server | Returns: object -- API response.
server.getServers
| Parameter | Type | Required | Description |
|---|---|---|---|
| (none) | No | Call with no arguments |
getRoutes
List all routes registered on a server
Module: server | Returns: object -- API response.
server.getRoutes
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | No | Server identifier |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
Server id is required | Check the error message for details |
handler must be a function | Check the error message for details |
path is required | Check the error message for details |
dir is required | Check the error message for details |
res is required | Check the error message for details |
html must be a string | Check the error message for details |
path is not a file | Check the error message for details |
url is required | Check the error message for details |
@desc "Create and validate result"
do
set $result as server.create
if $result != null
print "Success"
else
print "No result"
end
enddo
Recipes
1. List and iterate Servers
Retrieve all items and loop through them.
@desc "Get servers and iterate results"
do
set $result as server.getServers
each $item in $result
print $item
end
enddo
2. Create a new item with create
Create a new resource and capture the result.
set $result as server.create
print "Created: " + $result
3. Check before creating
List existing items and only create if needed.
@desc "Get servers and create"
do
set $existing as server.getServers
if $existing == null
server.create
print "Item created"
else
print "Item already exists"
end
enddo
4. Multi-step Server workflow
Chain multiple server operations together.
@desc "Create, start, and more"
do
set $r_create as server.create
set $r_start as server.start
set $r_stop as server.stop
print "All operations complete"
enddo
5. Safe create with validation
Check results before proceeding.
@desc "Create and validate result"
do
set $result as server.create
if $result != null
print "Success: " + $result
else
print "Operation returned no data"
end
enddo
Related Modules
- json -- JSON module for complementary functionality
Versions (3)
| Version | Tag | Published |
|---|---|---|
| 0.1.3 | latest | 21 days ago |
| 0.1.2 | 21 days ago | |
| 0.1.1 | 1 months ago |
Related Modules
ftp
JS@robinpathv0.1.3
FTP and SFTP file transfer operations
http
JS@robinpathv0.1.3
HTTP server for RobinPath scripts. Register routes with static responses (JSON, HTML, files), enable CORS, serve static directories. No callbacks needed.
form
JS@robinpathv0.2.1
Declarative form builder for RobinPath scripts � define fields inline, generate schemas, validate submissions
api
JS@robinpathv0.1.2
HTTP client for making requests to external APIs with profiles, auth, download/upload, and auto-JSON parsing
$ robinpath add @robinpath/server
