svrjs-nextjs-website/pages/docs/mods/mod-development-legacy.md

3.3 KiB

title
Mod development (.tar.gz mods)

Mod development (.tar.gz mods)

Mods in SVR.JS have two methods:

  • callback - Invoked on non-CONNECT requests (includes proxy requests using GET or POST methods). Parameters (must be in this particular order, argument names given to match SVR.JS API documentation):
    • req
    • res
    • serverconsole
    • responseEnd
    • href
    • ext
    • uobject
    • search
    • defaultpage
    • users
    • page404
    • head
    • foot
    • fd
    • elseCallback
    • configJSON
    • callServerError
    • getCustomHeaders
    • origHref
    • redirect
    • parsePostData
    • authUser

This method is required (if it is not present, SVR.JS will simply return 500 Internal Server Error on all requests with error message in error stack similar to "TypeError: modO.callback is not a function").

  • proxyCallback - Invoked on CONNECT requests (used for proxying). Parameters (must be in this particular order, argument names given to match SVR.JS API documentation):
    • req
    • socket
    • head
    • configJSON
    • serverconsole
    • elseCallback

Required in order for function returned from callback method to be invoked for request URLs beginning with "http://" or with "https://" (proxy through GET or POST method, non-proxy requests have request URLs beginning with "/"). You should implement a proxy URL check in callback method, if you're going to use proxyCallback and callback methods at once, or else your SVR.JS mod may be vulnerable to access control bypass attacks (SVR.JS doesn't enforce URL rewriting, custom headers and non-standard codes for proxy requests to avoid interference of its access controls with proxy mods).

These methods are defined inside Mod.prototype object. Both methods return a function, which will be executed in SVR.JS.

__dirname and . in require() function both refer to directory, to which mod contents are extracted.

The reference to file in the SVR.JS installation directory is __dirname + "/../../../filename" (replace filename with your desired file name).

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

A typical index.js file for a mod may look like this:

//Requires go here
function Mod() {}
Mod.prototype.callback = function callback(
	req,
	res,
	serverconsole,
	responseEnd,
	href,
	ext,
	uobject,
	search,
	defaultpage,
	users,
	page404,
	head,
	foot,
	fd,
	elseCallback,
	configJSON,
	callServerError,
	getCustomHeaders,
	origHref,
	redirect,
	parsePostData
) {
	return function () {
		//Mod contents go here
		if (href == "/hello.svr") {
			serverconsole.resmessage("Sent Hello World message!");
			res.writeHead(200, "OK", {
				"Content-Type": "text/plain",
			});
			res.end("Hello World!");
		} else {
			elseCallback();
		}
	};
};

//OPTIONAL: proxyCallback method
//Uncomment code below, if you want to use proxyCallback method.
//But then you'll need to implement proxy request URL check for callback method.

/*
Mod.prototype.proxyCallback = function proxyCallback(req, socket, head, configJSON, serverconsole, elseCallback) {
 return function () {
 //Just pass elseCallback
 elseCallback();
 }
}
*/

module.exports = Mod; //SVR.JS mod exports

The mod.info file (in JSON format) contains metadata about the mod, such as its name and version:

{
  "name": "The Example Mod",
  "version": "0.1.0"
}
502 Bad Gateway

502 Bad Gateway

The server had an error while it was acting as a gateway.

You may need to contact the server administrator at webmaster[at]svrjs[dot]org.

SVR.JS on git.svrjs.org