JSGI Requests and Responses
The JSGI (JavaScript Gateway Interface) specification defines an low-level interface between a HTTP server and JavaScript-based web applications. It has been developed by the CommonJS project and is implemented by Ringo. The main goal is to map incoming HTTP requests to arbitrary JavaScript objects. Application developers can use the request data, manipulate it, and generate a response object, which the JSGI implementation maps back to a valid HTTP response.
Between the HTTP server and the application, developers can include middleware functions. Middlewares can manipulate the request object, like parsing query parameters. They help to make application development easier. Web frameworks like stick provide a rich set of middlewares.
Request Object
host— host name of the web serverport— local port number of the web serverqueryString— query string of the URL, separated from the preceding path by a question markversion— HTTP version of the request in form of an array[majorVersion, minorVersion]remoteAddress— fully qualified name (FQDN) of the client's IPv6 or IPv4 addressscheme— returnshttpsfor secure connections,httpotherwiseasync— true if the request supports asynchronous operationheaders— object containing the client-supplied HTTP request headers with lower-case keys; multiple header values will be merged following RFC 7230input— body of the request in binary form asio.StreamscriptName— empty string if the current JSGI application is the "root" of the server; the initial portion of the request URL's path that corresponds to the application object otherwisepathInfo— the URL's decoded path, always starting with a "/" charactermethod— the name of the HTTP method; e.g.GET,HEAD,POST,PUT,DELETEenv— Ringo-specific environment informationenv.app— themodule.idif the current JSGI application is a CommonJS module; not defined otherwiseenv.servlet— the JSGIServlet instance handling this requestenv.servletRequest— the originaljavax.servlet.http.HttpServletRequestinstance provided by the Servlet containerenv.servletResponse— the originaljavax.servlet.http.HttpServletResponseinstance provided by the Servlet container
jsgi— JSGI-specific read-only variablesjsgi.version— Ringo implements version0.3jsgi.multithread— Ringo supports multi-threading, alwaystruejsgi.multiprocess— Ringo is a single-process runtime, alwaysfalsejsgi.runOnce— Ringo might call a single JSGI application multiple times, alwaysfalsejsgi.cgi— Ringo runs atop Jetty or any other Servlet container, but not atop CGI, so alwaysfalsejsgi.errors— referencessystem.stderrfor error output
Response Object
A JSGI application is a JavaScript function accepting exactly one argument which is the request object. It must return a valid response object. The response object has exactly three required properties:
status— a three-digit integer representing the HTTP response status code; e.g.200headers— a JavaScript object containing key/value pairs with string or array valuesbody— an array-like object implementing aforEachmethod, which will be called to generate a valid response. Each element must have atoByteStringmethod or be an instance ofBinary.