asterisk-server v1.0.9
Asterisk Server
Asterisk Server is an easy-to-use package for hosting local servers over http.
Features
- API Endpoints
- HTTP Redirects
- IP-based locks
- Zero dependencies
- Directory Redirecting
Documentation
NOTE: All variable/function descriptions are inclosed within definitons
Example usage:
// Require the asterisk-server module
const ast = require("asterisk-server");
// Open the server on port 8080
ast.server.open(8080);
// Set an error 404 page ("/error404.html")
ast.files.registerStatusPage(404, "/error404.html");
// Redirect all requests from "/favicon.ico" to "/favicon.svg"
ast.files.addRedirect("/favicon.ico", "/favicon.svg");
// Set the API prefix to be "/api/"
ast.api.setPrefix("/api/");
// Create an endpoint at path: "/api/example"
ast.api.createEndpoint(function(dataIn, IP){
let dataOut = {
dataYouSent: dataIn,
yourIP: IP,
message: "Hello from asterisk-server!",
status: 200
};
return dataOut;
}, "/example");
General
getIP()
Get IP of current machine
Returns
string
isThisMachine(<IP>)
Detect if an IP is equal to this device
Parameter
<IP>
type:string
Returns
boolean
Serverside
server.open(<PORT>)
Open a port on the current machine, on any specified port number
Parameter
<PORT>
type:number
API
api.createEndpoint(<CALLBACK>, <PATH>)
Creates an API endpoint
Parameter
<CALLBACK>
type:function(<DATA>, <IP>)
Parameter
<DATA>
type:object
Parameter
<IP>
type:string
Returns
{ content?: object, status?: number | 200, type?: string | "application/json" }
Parameter
<PATH>
type:string
api.endpoints
type: object[]
!INTERNAL VARIABLE! DO NOT USE
api.handleRequest()
!INTERNAL FUNCTION! DO NOT USE
api.lockIP()
Locks all API endpoints for all devices that are not hosting the server
api.unlockIP()
Unlocks all API endpoints, all devices can use every endpoint
lockedIP
type: boolean
api.setPrefix(<PREFIX>)
Default: "/api/"
Parameter
<PREFIX>
type:string
Files
files.hideAll()
Disable all file access, for all devices
files.showAll()
Enables all file access, for all devices
files.visibility
Type:
boolean
Default:
true
files.lockIP()
Disables all file access, for all devices other than the hosting device
files.unlockIP()
Enables all file access, for all devices other than the hosting device
files.lockedIP
Type:
boolean
Default:
false
files.registerStatusPage(<STATUS>, <PATH>)
Register a web page for a certain http status code
Eg: Error 404 -> "/status404.html"
Parameter
<STATUS>
type:number
Parameter
<PATH>
type:string
files.statusPages
Type:
boolean
Default:
{ 403: "/asterisk-server/403.html", 404: "/asterisk-server/404.html" }