net.listen Web Service

net.listen Web Service

net.listen Web Service

Function

net.listen({type: 'web'}) starts the BT Web service. Each request executes the site entry BT file and injects the web request context.

Syntax

Parameters

FieldTypeRequiredDefault valueDescription
typeStringYesNoneFixed to web.
bindStringNo0.0.0.0:8080Listening address.
sitesArrayYesNoneArray of site configurations, at least one.
sites[].rootStringNoweb/Site root directory.
sites[].entryStringNomain.btRequest entry BT file.
sites[].domainsArrayNo[]Host filter domain name.
sites[].upload.tempStringNotemp/Temporary directory for uploading files.
sites[].staticObjectNoNoneStatic-file configuration object; see the static fields below.
sites[].sslObjectNoNoneTLS certificate configuration object; see the ssl fields below.

The sites field

sites is the site configuration array. Each site object field is as follows:

FieldTypeRequiredDefault valueDescription
rootStringNoweb/Site root directory. Relative paths such as entry scripts and default static directories will be resolved based on this directory.
entryStringNomain.btBT entry file executed for each dynamic request.
domainsArrayNo[]Host filter domain name list. When it is an empty array, it is used as the default site without domain name restrictions.
uploadObjectNoNoneUpload configuration object; see the upload fields below.
staticObjectNoNoneStatic file configuration object. Passing in this object enables static file serving.
sslObjectNoNoneTLS certificate configuration object. Both cert and key must be provided to enable HTTPS.

upload field

FieldTypeRequiredDefault valueDescription
tempStringNotemp/Temporary storage directory for uploaded files. web.files[*].path and web.files[*].file will point to the saved file here.

static field

FieldTypeRequiredDefault valueDescription
routeStringNo/static/{**}Static resource route matching rules.
pathStringNoroot/staticStatic file directory. If not filled in, use the static directory under the root of the current site.
defaultStringNoindex.htmlThe default file name that is attempted to be returned when accessing the directory.
listBoolNofalseWhether to allow directory list display. Production environments usually keep false.
cache_controlStringNoEmpty stringThe static file responded successfully to the Cache-Control header written. When empty, the header is not actively written.
chunk_sizeIntNo0The number of bytes read in chunks of static files. Less than or equal to 0 uses the framework default value of 1048576.

ssl field

FieldTypeRequiredDefaultDescription
certStringYesNonePath to the TLS certificate file.
keyStringYesNoneTLS private key file path.

Return Value

TypeDescription
WebServerReturns the Web service handle.

WebServer fields and methods

NameTypeDescription
addrStringActual listening address, usually in the format host:port.
typeStringFixed to web.
close()Fn -> BoolClose the background web service and return true successfully.

Examples

Notes

  • The relative paths of root, upload.temp, static.path, ssl.cert, ssl.key are resolved based on the directory where the source code file is called; @ represents the project root.
- TLS will not be enabled when either ssl.cert and ssl.key are empty. - Static files support ETag, Last-Modified, conditional requests and HTTP Range by default; cache_control is only responsible for additional write caching strategies.
  • chunk_size controls the block read size of static files and does not read large files into memory at once.