Modules@robinpath/http
http

@robinpath/http

0.1.3Node.jsPublic

HTTP server for RobinPath scripts. Register routes with static responses (JSON, HTML, files), enable CORS, serve static directories. No callbacks needed.

HTTP

HTTP server for RobinPath scripts. Register routes with static responses (JSON, HTML, files), enable CORS, serve static directories. No callbacks needed.

Package: @robinpath/http | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the http module when you need to:

  • Create a new HTTP server instance (does not start listening yet) -- Use http.createServer to perform this operation
  • Register a GET route that returns static JSON data -- Use http.get to perform this operation
  • Register a POST route that returns static JSON data -- Use http.post to perform this operation
  • Register a PUT route that returns static JSON data -- Use http.put to perform this operation
  • Register a DELETE route that returns static JSON data -- Use http.delete to perform this operation

Quick Reference

FunctionDescriptionReturns
createServerCreate a new HTTP server instance (does not start listening yet)Server creation info
getRegister a GET route that returns static JSON dataRoute registration info
postRegister a POST route that returns static JSON dataRoute registration info
putRegister a PUT route that returns static JSON dataRoute registration info
deleteRegister a DELETE route that returns static JSON dataRoute registration info
htmlRegister a GET route that serves an HTML stringRoute registration info
fileRegister a GET route that serves a file from diskRoute registration info
redirectRegister a route that redirects to another URLRoute registration info
staticRegister a directory to serve static files fromStatic directory registration info
corsEnable CORS on the serverCORS configuration info
listenStart the HTTP server listening for requestsServer listening info with URL
stopStop the HTTP server gracefullyServer stop confirmation
statusGet server status: port, routes, listening state, request countServer status info
logsGet the request log for a serverArray of request log entries

Functions

createServer

Create a new HTTP server instance (does not start listening yet)

Module: http | Returns: object -- Server creation info

any "myapi" {"port": 8080}
ParameterTypeRequiredDescription
idstringYesUnique server identifier
optsobjectNoOptions: {port?, host?, logRequests?}

get

Register a GET route that returns static JSON data

Module: http | Returns: object -- Route registration info

any "myapi" "/api/products" [{"id": 1}]
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path pattern
dataanyYesJSON data to return
optsobjectNoOptions: {status?, headers?}

post

Register a POST route that returns static JSON data

Module: http | Returns: object -- Route registration info

any "myapi" "/api/products" {"created": true}
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path pattern
dataanyYesJSON data to return
optsobjectNoOptions: {status?, headers?}

put

Register a PUT route that returns static JSON data

Module: http | Returns: object -- Route registration info

any "myapi" "/api/products/:id" {"updated": true}
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path pattern
dataanyYesJSON data to return
optsobjectNoOptions: {status?, headers?}

delete

Register a DELETE route that returns static JSON data

Module: http | Returns: object -- Route registration info

any "myapi" "/api/products/:id" {"deleted": true}
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path pattern
dataanyYesJSON data to return
optsobjectNoOptions: {status?, headers?}

html

Register a GET route that serves an HTML string

Module: http | Returns: object -- Route registration info

any "myapi" "/" "Hello World"
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path
contentstringYesHTML string to serve
optsobjectNoOptions: {status?, headers?}

file

Register a GET route that serves a file from disk

Module: http | Returns: object -- Route registration info

any "myapi" "/report" "C:/reports/report.pdf"
ParameterTypeRequiredDescription
idstringYesServer ID
pathstringYesURL path
filePathstringYesAbsolute file path on disk
optsobjectNoOptions: {status?, headers?}

redirect

Register a route that redirects to another URL

Module: http | Returns: object -- Route registration info

any "myapi" "/old" "/new" {"status": 301}
ParameterTypeRequiredDescription
idstringYesServer ID
fromPathstringYesURL path to redirect from
toUrlstringYesTarget URL to redirect to
optsobjectNoOptions: {status? (301 or 302)}

static

Register a directory to serve static files from

Module: http | Returns: object -- Static directory registration info

any "myapi" "C:/mysite/public"
ParameterTypeRequiredDescription
idstringYesServer ID
dirPathstringYesAbsolute directory path
optsobjectNoOptions (reserved)

cors

Enable CORS on the server

Module: http | Returns: object -- CORS configuration info

any "myapi" {"origin": "http://localhost:5173"}
ParameterTypeRequiredDescription
idstringYesServer ID
optsobjectNoOptions: {origin?, methods?, headers?}

listen

Start the HTTP server listening for requests

Module: http | Returns: object -- Server listening info with URL

any "myapi" 8080
ParameterTypeRequiredDescription
idstringYesServer ID
portnumberNoPort number (default 3000)
optsobjectNoOptions: {host?}

stop

Stop the HTTP server gracefully

Module: http | Returns: object -- Server stop confirmation

any "myapi"
ParameterTypeRequiredDescription
idstringYesServer ID

status

Get server status: port, routes, listening state, request count

Module: http | Returns: object -- Server status info

any "myapi"
ParameterTypeRequiredDescription
idstringYesServer ID

logs

Get the request log for a server

Module: http | Returns: array -- Array of request log entries

any "myapi" {"limit": 10, "method": "GET"}
ParameterTypeRequiredDescription
idstringYesServer ID
optsobjectNoFilter options: {limit?, method?, path?}

Error Handling

All functions throw on failure. Common errors:

ErrorCause
HTTP server not foundCheck the error message for details
@desc "Validate result"
do
  set $result as any "myapi" {"port": 8080}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Iterate results"
do
  set $result as any "myapi" "/api/products" [{"id": 1}]
  each $item in $result
    print $item
  end
enddo

2. Create a new item with createServer

Create a new resource and capture the result.

set $result as any "myapi" {"port": 8080}
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "Validate result"
do
  set $existing as any "myapi" "/api/products" [{"id": 1}]
  if $existing == null
    any "myapi" {"port": 8080}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step HTTP workflow

Chain multiple http operations together.

@desc "Execute operation"
do
  set $r_createServer as any "myapi" {"port": 8080}
  set $r_get as any "myapi" "/api/products" [{"id": 1}]
  set $r_post as any "myapi" "/api/products" {"created": true}
  print "All operations complete"
enddo

5. Safe createServer with validation

Check results before proceeding.

@desc "Validate result"
do
  set $result as any "myapi" {"port": 8080}
  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/http

Collaborators

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

Keywords

Category

web