SVR.JS documentation
Server-side JavaScript
Server-side JavaScript

Server-side JavaScript

Another way to expand SVR.JS functionality is through server-side JavaScript located in serverSideScript.js file inside SVR.JS web root (or locaten in SVR.JS installation directory if you're running SVR.JS 3.9.0 or newer, and you have set useWebRootServerSideScript property to false). Server-side JavaScript allows you to create various web applications using JavaScript, Node.JS and SVR.JS API.

Predefined objects

When working with server-side JavaScript in SVR.JS, you have access to several predefined objects that can greatly enhance your scripting capabilities. These objects are available for use without requiring any additional imports or installations.

  • readline: The readline library for reading input streams.
  • os: The os library for accessing operating system-related information.
  • http: The http library for handling HTTP functionality.
  • url: The url library for parsing and formatting URLs.
  • fs: The fs library for interacting with the file system.
  • path: The path library for working with file and directory paths.
  • hexstrbase64: The hexstrbase64 library for converting hexadecimal strings to base64 format.
  • crypto: The crypto library for cryptographic functionality, such as generating hashes and handling encryption.
  • https: The https library, an extension of the http library for handling secure HTTPS connections.
  • stream: The stream library for working with streams of data.
  • customvar1, customvar2, customvar3, customvar4: Custom variables meant to be reused in multiple requests. These variables are available starting from SVR.JS version 3.0.0.

Additionally, there is an option to control the automatic execution of the elseCallback function. By default, automatic execution is enabled. You can use the disableEndElseCallbackExecute option to disable this behavior if needed.

By leveraging these predefined objects, you can streamline your server-side JavaScript code and build powerful applications using SVR.JS.

Predefined methods

See methods in SVR.JS API in non-proxy section

Additionally SVR.JS server-side JavaScript has filterHeaders(headers) method, that filters out invalid request headers.

SVR.JS 3.8.0 and newer have additionally two methods:

  • checkHref(destHref) - checks if request path name matches the destHref.
  • checkHostname(hostname) - checks if host name defined in the request matches the hostname parameter.

SSJS development

__dirname and . in require() function both refer to temp directory in SVR.JS.

Current working directory (process.cwd()) is SVR.JS web root.

If you want to divide server-side JavaScript into several files, you can do one of those:

  • require(process.cwd() + "/someOtherScript.js") for script located in the web root (it's recommended to set up 403 Forbidden or 404 Not Found non-standard code in SVR.JS configuration to prevent source code leaks).
  • require("../someOtherScript.js") for script located in SVR.JS installation directory.
  • require("/usr/lib/webappfiles/someOtherScript.js") for script located in /usr/lib/webappfiles.

Example code:

disableEndElseCallbackExecute = true; //Without "var", else it will not work!!!
if (href == "/hello.svr") {
	serverconsole.resmessage("Sent Hello World message!");
	res.writeHead(200, "OK", {
		"Content-Type": "text/plain",
	});
	res.end("Hello World!");
} else {
	elseCallback();
}