1.0.9 • Published 2 years ago

sprucehttp_sjs v1.0.9

Weekly downloads
-
License
Copyright (c) STi...
Repository
-
Last release
2 years ago

SpruceHTTP SJS:

A module for responding to requests within SpruceHTTP in an SJS file.

Table of Contents:

Documentation:

Importing the module:

const sjs = require("sprucehttp_sjs");

streamMode():

Sets the response into stream mode. This cannot be undone for the duration of the script, and should be the first action performed if this is the intended mode.

Stream mode means that responses are not buffered by the webserver, and thus are sent immediately as they're received from the SJS script. This means that the status line and headers must be sent first before the data, and the \r\n after the headers must be sent by the script as well.

sjs.streamMode(); // Set the response to stream mode.

writeStatusLine(statusCode[, reasonPhrase]):

  • statusLine: number: The HTTP status code to send.
  • reasonPhrase: string: The reason phrase to send.

Writes the status line to the response.

To send a 200 "OK" response, without needing to specify the reason phrase.

sjs.writeStatusLine(200);

To send a 418 "I'm a teapot" response, with a specified reason phrase.

sjs.writeStatusLine(418, "I'm a teapot");

writeHeader(name, value):

  • name: string: The name of the header to send.
  • value: string: The value of the header to send.

Writes a header to the response.

To write the "Content-Type: text/html" header.

sjs.writeHeader("Content-Type", "text/html");

writeData(data):

  • data: string | Buffer: The data to send.

Writes data (the body) to the response. When not in stream mode, the Content-Length header is automatically updated.

Writes text data to the body.

sjs.writeData("Hello, world!");

Writes binary data (a picture, in this case) to the body.

const fs = require('fs');
sjs.writeData(fs.readFileSync("image.png", 'binary'));

writeDataAsync(data):

  • data: string | Buffer: The data to send.

Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.

Writes text data to the body.

await sjs.writeDataAsync("Hello, world!");

Writes binary data (a picture, in this case) to the body.

const fs = require('fs');
await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));

clearResponse():

Clears the currently written response and resets the status line, headers, and body. This is useful when you want to send a different response than the one you have already written.

This function does not work in stream mode.

sjs.writeStatusLine(418, "I'm a teapot");
sjs.writeHeader("Content-Type", "text/html");
sjs.writeData("<h1>I'm a teapot</h1>");
// Be serious
sjs.clearResponse();

sjs.writeStatusLine(200);
sjs.writeHeader("Content-Type", "text/html");
sjs.writeData("<h1>I'm <i>not</i> a teapot</h1>");

siteConfig():

Returns the site-specific configuration set in your config file.

console.log(sjs.siteConfig());
/*
{
	"type": "local",
	"location": "./",
	"upgradeInsecure": true,
	"directoryListing": true,
	"sjs": true,
	"headers": {
		"Access-Control-Allow-Origin": "*"
	},
	"ssl": {
		"key": "./ssl/key.pem",
		"cert": "./ssl/cert.pem",
		"minVersion": "TLSv1.2",
		"maxVersion": "TLSv1.3"
	}
}
*/

requestIP():

Returns the requestor's IP address.

console.log(sjs.requestIP());
// ::ffff:127.0.0.1

method():

Returns the request method.

console.log(sjs.method());
// get

path():

Returns the request path.

console.log(sjs.path());
// /index.sjs

pathInfo():

Returns the request's info path.

// Using path /index.sjs/info
console.log(sjs.path());
console.log(sjs.pathInfo());
// /index.sjs
// /info

query():

Returns the request query.

console.log(sjs.query());
/*
{
	"name": "John",
	"age": "42"
}
*/

queryValue(key):

  • key: string: The key of the query to get.

Returns the value of a query parameter.

console.log(sjs.queryValue("name"));
// John

httpVersion():

Returns the HTTP version of the request.

console.log(sjs.httpVersion());
// http/1.1

headers():

Returns the request headers.

console.log(sjs.headers());
/*
{
	"connection": "keep-alive",
	"host": "localhost:8080",
	"upgrade-insecure-requests": "1",
	"user-agent": "Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0"
}
*/

headerValue(name):

  • name: string: The name of the header to get.

Returns the value of a header.

console.log(sjs.headerValue("user-agent"));
// Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0

body():

Returns the request body as a buffer.

console.log(sjs.body().toString("utf8"));
// Hello, world!

bodyStream():

Returns the request body as a ReadStream.

console.log(sjs.bodyStream().read(13).toString("utf8"));
// Hello, world!
1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago