src/nodejs/jshttp

    Dark Mode
Search:
Group by:

  • Async HTTP / HTTPS Web Server.

Example: cmd: -r:off -b:js --experimental:strictFuncs

import src/nodejs/jshttp
import std/jsconsole
requireHttp()  ## Example is "Verbose on Purpose" to show types, names, etc.

func listener(request: HttpClientRequest; response: HttpServerResponse) =
  response.writeHead(statusCode = 200, statusMessage = "OK".cstring,
    headers = {"Content-Type".cstring: "text/html".cstring, "DNT".cstring: "1".cstring})
  response.write("<h1> Hello World </h1>".cstring)
  response.ends("<hr>".cstring)

proc example() =
  console.log "This is an example"

let server: HttpServer = createServer(requestListener = listener)
server.onRequest((proc () = console.log "Hello"))
server.listen(port = 8000, host = "127.0.0.1", callback = example)

Example: cmd: -r:off -b:js --experimental:strictFuncs

import src/nodejs/jshttp
import std/jsconsole # "HTTPS" example.
requireHttp()   ## Has all methods for both HTTP and HTTPS, DRY.
requireHttps()  ## Has the HTTPS web server type only.

func listener(request: HttpClientRequest; response: HttpServerResponse) =
  response.writeHead(statusCode = 200, statusMessage = "OK".cstring,
    headers = {"Content-Type".cstring: "text/html".cstring, "DNT".cstring: "1".cstring})
  response.write("<h1> Hello World </h1>".cstring)
  response.ends("<hr>".cstring)

proc example() =
  console.log "This is an example"

let server: HttpsServer = createServerHttps(requestListener = listener)  # See "HTTPS" in name.
server.onRequest((proc () = console.log "Hello"))
server.listen(port = 8000, host = "127.0.0.1", callback = example)

Types

HttpAgent {.importjs: "http.Agent".} = ref object of JsRoot
  freeSockets*, requests*, sockets*: JsObject
  maxFreeSockets*, maxSockets*, maxTotalSockets*: cint
https://nodejs.org/api/http.html#http_class_http_agent
HttpClientRequest {.importjs: "http.ClientRequest".} = ref object of JsRoot
  aborted*, destroyed*, finished*, reusedSocket*, writableEnded*,
  writableFinished*: bool
  metod* {.importjs: "method".}: cstring
  path*, host*, protocol*: cstring
  maxHeadersCount*: cint
https://nodejs.org/api/http.html#http_class_http_clientrequest
HttpIncomingMessage {.importjs: "http.IncomingMessage".} = ref object of JsRoot
  aborted*, complete*: bool
  headers*, trailers*: JsObject
  metod* {.importjs: "method".}: cstring
  httpVersion*, statusMessage*, url*: cstring
  rawHeaders*, rawTrailers*: seq[cstring]
  statusCode*: cint
https://nodejs.org/api/http.html#http_class_http_incomingmessage
HttpOutgoingMessage {.importjs: "http.OutgoingMessage".} = ref object of JsRoot
  headersSent*, writableEnded*, writableFinished*, writableObjectMode: bool
  writableCorked*, writableHighWaterMark*, writableLength*: cint
https://nodejs.org/api/http.html#http_class_http_outgoingmessage
HttpServer {.importjs: "http.Server".} = ref object of JsRoot
  headersTimeout*, maxHeadersCount*, requestTimeout*, timeout*,
  keepAliveTimeout*, defaultMaxListeners*, maxConnections*: cint
  listening*, captureRejections*: bool
  errorMonitor*: cstring
https://nodejs.org/api/http.html#http_class_http_server
HttpServerResponse {.importjs: "http.ServerResponse".} = ref object of JsRoot
  finished*, headersSent*, sendDate*, writableEnded*, writableFinished*: bool
  statusCode*: cint
  statusMessage*: cstring
HttpsServer {.importjs: "https.Server".} = ref object of JsRoot
  headersTimeout*, maxHeadersCount*, requestTimeout*, timeout*,
  keepAliveTimeout*, defaultMaxListeners*, maxConnections*: cint
  listening*, captureRejections*: bool
  errorMonitor*: cstring
https://nodejs.org/api/http.html#http_class_http_server

Procs

func abort(self: HttpClientRequest) {.importjs: "#.$1()", discardable,
                                      ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_abort
func address(self: HttpServer or HttpsServer): cstring {.importjs: "#.$1()",
    ...raises: [], tags: [].}
func addTrailers(self: HttpServerResponse or HttpOutgoingMessage;
                 headers: JsObject) {.importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_addtrailers_headers
func close(self: HttpServer or HttpsServer) {.importjs: "#.$1()", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_server_close_callback
func close[T](self: HttpServer or HttpsServer; callback: T) {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_close_callback
func cork(self: HttpServerResponse or HttpOutgoingMessage) {.importjs: "#.$1()",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_cork
func createServer(options: JsObject): HttpServer {.importjs: "http.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServer[T](options: JsObject; requestListener: T): HttpServer {.
    importjs: "http.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServer[T](requestListener: T): HttpServer {.importjs: "http.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServerHttps(): HttpsServer {.importjs: "https.createServer()",
                                        ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServerHttps(options: JsObject): HttpsServer {.
    importjs: "https.createServer(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServerHttps[T](options: JsObject; requestListener: T): HttpsServer {.
    importjs: "https.createServer(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func createServerHttps[T](requestListener: T): HttpsServer {.
    importjs: "https.createServer(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
func destroy(self: HttpAgent or HttpIncomingMessage or HttpOutgoingMessage or
    HttpClientRequest): auto {.importjs: "#.$1()", discardable, ...raises: [],
                               tags: [].}
https://nodejs.org/api/http.html#http_agent_destroy
func emit(self: HttpServer or HttpsServer; eventName: cstring; args: auto) {.
    importjs: "#.emit(#, #)", varargs, discardable, ...raises: [], tags: [].}
func ends(self: HttpClientRequest or HttpServerResponse or HttpOutgoingMessage): auto {.
    importjs: "#.end()", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_end_data_encoding_callback
func ends(self: HttpClientRequest or HttpServerResponse or HttpOutgoingMessage;
          data: cstring or Buffer; encoding = "utf-8".cstring): auto {.
    importjs: "#.end(#, #)", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_end_data_encoding_callback
func ends[T](self: HttpClientRequest or HttpServerResponse or
    HttpOutgoingMessage; callback: T): auto {.importjs: "#.end(#)", discardable,
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_end_data_encoding_callback
func get(url: cstring): HttpClientRequest {.importjs: "http.$1(#)", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func get[T](options: JsObject; callback: T): HttpClientRequest {.
    importjs: "http.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func get[T](url: cstring; options: JsObject; callback: T): HttpClientRequest {.
    importjs: "http.$1(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func getHeader(self: HttpClientRequest or HttpServerResponse; name: cstring): auto {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_getheader_name
func getHeaderNames(self: HttpServerResponse or HttpOutgoingMessage): seq[
    cstring] {.importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_getheadernames
func getHeaders(self: HttpServerResponse or HttpOutgoingMessage): JsObject {.
    importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_getheaders
func getHttps(url: cstring): HttpClientRequest {.importjs: "https.get(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func getHttps[T](options: JsObject; callback: T): HttpClientRequest {.
    importjs: "https.get(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func getHttps[T](url: cstring; options: JsObject; callback: T): HttpClientRequest {.
    importjs: "https.get(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_get_options_callback
func getMaxListeners(self: HttpServer or HttpsServer): cint {.
    importjs: "#.$1()", ...raises: [], tags: [].}
func getName(self: HttpAgent; options: JsObject): cstring {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_agent_getname_options
func getRawHeaderNames(self: HttpClientRequest): seq[cstring] {.
    importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_getrawheadernames
func hasHeader(self: HttpServerResponse or HttpOutgoingMessage; name: cstring): bool {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_hasheader_name
func importHttp() {.importjs: "import * as http from \'http\'@", ...raises: [],
                    tags: [].}
Alias for import * as module_name from 'module_name';. Must be called once before using the module
func importHttps() {.importjs: "import * as https from \'https\'@", ...raises: [],
                     tags: [].}
Alias for import * as module_name from 'module_name';. Must be called once before using the module
func listen(self: HttpServer or HttpsServer) {.importjs: "#.$1()", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen(self: HttpServer or HttpsServer; handle: JsObject; backlog: cint) {.
    importjs: "#.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen(self: HttpServer or HttpsServer; port: int) {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen(self: HttpServer or HttpsServer; port: int; host: cstring) {.
    importjs: "#.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen[T](self: HttpServer or HttpsServer; callback: T) {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen[T](self: HttpServer or HttpsServer; options: JsObject; callback: T) {.
    importjs: "#.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen[T](self: HttpServer or HttpsServer; path: cstring; backlog: cint;
               callback: T) {.importjs: "#.$1(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listen[T](self: HttpServer or HttpsServer; port: int; host: cstring;
               callback: T) {.importjs: "#.$1(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_server_listen
func listenerCount(self: HttpServer or HttpsServer; eventName: cstring): cint {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
func listeners(self: HttpServer or HttpsServer; eventName: cstring): seq[auto] {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
func newHttpAgent(): HttpAgent {.importjs: "(new Agent())@", ...raises: [],
                                 tags: [].}
https://nodejs.org/api/http.html#http_new_agent_options
func newHttpAgent(options: JsObject): HttpAgent {.importjs: "(new Agent(#))",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_new_agent_options
func off[T](self: HttpServer or HttpsServer; event: cstring; callback: T) {.
    importjs: "#.off(#, #)", discardable, ...raises: [], tags: [].}
func on[T](self: HttpServer or HttpsServer; event: cstring; callback: T) {.
    importjs: "#.on(#, #)", discardable, ...raises: [], tags: [].}
func once[T](self: HttpServer or HttpsServer; event: cstring; callback: T) {.
    importjs: "#.once(#, #)", discardable, ...raises: [], tags: [].}
func pipe(self: HttpOutgoingMessage) {.importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_outgoingmessage_pipe
func prependListener[T](self: HttpServer or HttpsServer; event: cstring;
                        callback: T) {.importjs: "#.$1(#, #)", discardable,
                                       ...raises: [], tags: [].}
func prependOnceListener[T](self: HttpServer or HttpsServer; event: cstring;
                            callback: T) {.importjs: "#.$1(#, #)", discardable,
    ...raises: [], tags: [].}
func rawListeners(self: HttpServer or HttpsServer; event: cstring): seq[auto] {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
func removeAllListeners(self: HttpServer or HttpsServer; event: cstring) {.
    importjs: "#.$1(#)", discardable, ...raises: [], tags: [].}
func removeHeader(self: HttpClientRequest or HttpServerResponse or
    HttpOutgoingMessage; name: cstring): bool {.importjs: "#.$1(#)", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_request_removeheader_name
func request(url: cstring): HttpClientRequest {.importjs: "http.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func request[T](options: JsObject; callback: T): HttpClientRequest {.
    importjs: "http.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func request[T](url: cstring; options: JsObject; callback: T): HttpClientRequest {.
    importjs: "http.$1(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func requestHttps(url: cstring): HttpClientRequest {.importjs: "https.get(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func requestHttps[T](options: JsObject; callback: T): HttpClientRequest {.
    importjs: "https.get(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func requestHttps[T](url: cstring; options: JsObject; callback: T): HttpClientRequest {.
    importjs: "https.get(#, #, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_request_options_callback
func requireHttp() {.importjs: "const http = require(\'http\')@", ...raises: [],
                     tags: [].}
Alias for const module_name = require('module_name');. Must be called once before using the module
func requireHttps() {.importjs: "const https = require(\'https\')@", ...raises: [],
                      tags: [].}
Alias for const module_name = require('module_name');. Must be called once before using the module
func setHeader(self: HttpClientRequest or HttpServerResponse; name: cstring;
               value: auto): auto {.importjs: "#.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_setheader_name_value
func setNoDelay(self: HttpClientRequest; noDelay: bool) {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_setnodelay_nodelay
func setSocketKeepAlive(self: HttpClientRequest; enable: bool;
                        initialDelay: cint) {.importjs: "#.$1(#, #)",
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_setsocketkeepalive_enable_initialdelay
func setTimeout[T](self: HttpClientRequest or HttpServer or HttpsServer or
    HttpIncomingMessage or
    HttpOutgoingMessage; timeout: cint; callback: T): auto {.
    importjs: "#.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback
func uncork(self: HttpServerResponse or HttpOutgoingMessage) {.
    importjs: "#.$1()", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_uncork
proc validateHeaderName(name: cstring) {.importjs: "http.$1(#)", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_http_validateheadername_name
Warning: May or may not raise a foreign Error, use try ... except
proc validateHeaderValue(name: cstring; value: auto) {.
    importjs: "http.$1(#, #)", ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_http_validateheadervalue_name_value
Warning: May or may not raise a foreign Error, use try ... except
func write(self: HttpClientRequest or HttpServerResponse or HttpOutgoingMessage;
           data: Buffer): bool {.importjs: "#.$1(#)", discardable, ...raises: [],
                                 tags: [].}
https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
func write(self: HttpClientRequest or HttpServerResponse or HttpOutgoingMessage;
           data: cstring; encoding = "utf-8".cstring): bool {.
    importjs: "#.$1(# + \'\\n\', #)", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
  • '\n' is automatically inserted at the end of data string.
func write[T](self: HttpClientRequest or HttpServerResponse or
    HttpOutgoingMessage; callback: T): bool {.importjs: "#.$1(#)", discardable,
    ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
func writeContinue(self: HttpServerResponse) {.importjs: "#.$1()", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_response_hasheader_name
func writeHead(self: HttpServerResponse; headers: JsObject or openArray[auto]): HttpServerResponse {.
    importjs: "#.$1(#)", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
func writeHead(self: HttpServerResponse; statusCode: int; statusMessage: cstring): HttpServerResponse {.
    importjs: "#.$1(#, #)", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
func writeHead(self: HttpServerResponse; statusCode: int;
               statusMessage: cstring; headers: openArray[(cstring, cstring)]): HttpServerResponse {.
    importjs: "#.$1(#, #)", discardable, ...raises: [], tags: [].}
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
func writeProcessing(self: HttpServerResponse) {.importjs: "#.$1()", ...raises: [],
    tags: [].}
https://nodejs.org/api/http.html#http_response_writeprocessing

Templates

template onCheckContinue(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("checkContinue"), callback).
template onCheckExpectation(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("checkExpectation"), callback).
template onClientError(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("clientError"), callback).
template onClose(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("close"), callback).
template onConnect(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("connect"), callback).
template onConnection(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("connection"), callback).
template onError(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("error"), callback).
template onListening(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("listening"), callback).
template onNewListener(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("newListener"), callback).
template onRemoveListener(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("removeListener"), callback).
template onRequest(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("request"), callback).
template onUpgrade(self: HttpServer or HttpsServer; callback)
Alias for httpServer.on(event = cstring("upgrade"), callback).