Modules@robinpath/server
server

@robinpath/server

0.1.3Node.jsPublic

HTTP 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.create to perform this operation
  • Start listening for incoming connections -- Use server.start to perform this operation
  • Stop the server and close all connections -- Use server.stop to perform this operation
  • Register a handler for all incoming requests -- Use server.onRequest to perform this operation
  • Register an error handler for the server -- Use server.onError to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate a new HTTP server instanceobject
startStart listening for incoming connectionsobject
stopStop the server and close all connectionsobject
onRequestRegister a handler for all incoming requestsobject
onErrorRegister an error handler for the serverobject
routeAdd a route with method, path pattern, and handlerobject
staticServe static files from a directoryobject
sendJsonSend a JSON responseobject
sendHtmlSend an HTML responseobject
sendFileSend a file as the responseobject
sendRedirectSend an HTTP redirect responseobject
statusSend a response with a specific status codeobject
corsEnable and configure CORS headers for a serverobject
getServersList all active server instancesobject
getRoutesList all routes registered on a serverobject

Functions

create

Create a new HTTP server instance

Module: server | Returns: object -- API response.

server.create
ParameterTypeRequiredDescription
idstringNoUnique server identifier
portnumberNoPort to listen on (default 3000)
hoststringNoHost to bind to (default 0.0.0.0)

start

Start listening for incoming connections

Module: server | Returns: object -- API response.

server.start
ParameterTypeRequiredDescription
idstringNoServer identifier

stop

Stop the server and close all connections

Module: server | Returns: object -- API response.

server.stop
ParameterTypeRequiredDescription
idstringNoServer identifier

onRequest

Register a handler for all incoming requests

Module: server | Returns: object -- API response.

server.onRequest
ParameterTypeRequiredDescription
idstringNoServer identifier
handlerstringNoRequest handler function (req, res)

onError

Register an error handler for the server

Module: server | Returns: object -- API response.

server.onError
ParameterTypeRequiredDescription
idstringNoServer identifier
handlerstringNoError handler function (err)

route

Add a route with method, path pattern, and handler

Module: server | Returns: object -- API response.

server.route
ParameterTypeRequiredDescription
idstringNoServer identifier
methodstringNoHTTP method (GET, POST, etc.)
pathstringNoRoute path pattern (supports :param and *)
handlerstringNoRoute handler function (req, res)

static

Serve static files from a directory

Module: server | Returns: object -- API response.

server.static
ParameterTypeRequiredDescription
idstringNoServer identifier
dirstringNoDirectory path to serve files from

sendJson

Send a JSON response

Module: server | Returns: object -- API response.

server.sendJson
ParameterTypeRequiredDescription
resobjectNoHTTP response object
dataanyNoData to serialize as JSON
statusnumberNoHTTP status code (default 200)

sendHtml

Send an HTML response

Module: server | Returns: object -- API response.

server.sendHtml
ParameterTypeRequiredDescription
resobjectNoHTTP response object
htmlstringNoHTML content
statusnumberNoHTTP status code (default 200)

sendFile

Send a file as the response

Module: server | Returns: object -- API response.

server.sendFile
ParameterTypeRequiredDescription
resobjectNoHTTP response object
pathstringNoPath to the file
statusnumberNoHTTP status code (default 200)

sendRedirect

Send an HTTP redirect response

Module: server | Returns: object -- API response.

server.sendRedirect
ParameterTypeRequiredDescription
resobjectNoHTTP response object
urlstringNoURL to redirect to
statusnumberNoHTTP status code (default 302)

status

Send a response with a specific status code

Module: server | Returns: object -- API response.

server.status
ParameterTypeRequiredDescription
resobjectNoHTTP response object
codenumberNoHTTP status code
bodystringNoResponse body text

cors

Enable and configure CORS headers for a server

Module: server | Returns: object -- API response.

server.cors
ParameterTypeRequiredDescription
idstringNoServer identifier
originstringNoAllowed origin (default *)
methodsstringNoAllowed methods
headersstringNoAllowed headers

getServers

List all active server instances

Module: server | Returns: object -- API response.

server.getServers
ParameterTypeRequiredDescription
(none)NoCall with no arguments

getRoutes

List all routes registered on a server

Module: server | Returns: object -- API response.

server.getRoutes
ParameterTypeRequiredDescription
idstringNoServer identifier

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Server id is requiredCheck the error message for details
handler must be a functionCheck the error message for details
path is requiredCheck the error message for details
dir is requiredCheck the error message for details
res is requiredCheck the error message for details
html must be a stringCheck the error message for details
path is not a fileCheck the error message for details
url is requiredCheck 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)

VersionTagPublished
0.1.3latest21 days ago
0.1.221 days ago
0.1.11 months ago
Install
$ robinpath add @robinpath/server

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.3
LicenseMIT
Unpacked Size14.7 KB
Versions3
Weekly Downloads21
Total Downloads21
Stars0
Last Publish21 days ago
Created1 months ago

Keywords

Category

web