Sempervirens Server
An Express server system for serving one or more website(s) from one server, it provides a file structure and pre-configurations for further configuring and building out websites and APIs.
Installation
npm i @sempervirens/server
Usage
- Run
npm init
in a root directory, and then define the following directory structure, including one or more websites.
/{root}
/node_modules
/sites
/site-1
/public
index.html
/site-2
/public
index.html
package-lock.json
package.json
server.js
- Implement the
Server
class in theserver.js
file.
server.js
import Server from '@sempervirens/server';
import { RequestHandler } from '@sempervirens/endpoint';
class Test1RequestHandler extends RequestHandler {
constructor({ req, res, isSecure }) {
super({ req, res, isSecure });
this.#init();
}
#init() {
this.res.send('Success 1');
}
}
const server = new Server({
sites: [
{
domain: 'site-1'
},
{
domain: 'site-2',
endpoints: [
{
path: 'GET /api/test-1',
handler: Test1RequestHandler
},
{
path: 'GET /page-1',
handler: ({ req, res, isSecure }) => {
res.send(`<html><head><title>Site 1</title><body>Full URL: ${req.fullUrl}</body></html>`);
}
}
]
}
]
});
server.start({
message: '',
suppressLog: false
});
- Run
node server
.
cd {root}
node server
API
constructor
Param | Type | Description |
---|---|---|
corsOptions |
object | Optional. Default: {} . See NPM cors package configuration options. |
httpRedirectServerPort |
number | Optiona. Default 80 . Sets the HTTP server port for the HTTP to HTTPS redirect functionality. Primarily for testing. |
enableRedirectToHttps |
boolean | Optional. Default: false . If true, then when a request arrives at "http://{domain}", it is redirected to "https://{domain}". "sslPaths" is required for "enableRedirectToHttps" to redirect. |
enableRedirectToWww |
boolean | Optional. Default: false . If true, then when a request arrives at "http(s)://{domain}", it is redirected to "http(s)://www.{domain}". |
middleware |
object[] | Optional. Default: [] . Shape: `{ path?: string; handler: RequestHandler |
port |
number | Optional. Default: 80 or 443 . The port to start the server on. The default server port is 80. If "sslPaths" is valid, then the default server port is 443. |
sites |
object | Required. Each object is passed into the SiteLoader constructor. See @sempervirens/site-loader for more information. When the the server starts: - Sites become available for local development at http://localhost/{domain} . - If only one site is given, then the site becomes available at http://localhost as well. - Sites are also available at http://{domain} , primarily for making them available online. - (If an entry has been added to the local OS's hosts file, then the site will load locally at http://{domain} , too.) |
sitesDir |
string | Optional. Default: sites . Sets the name of the {root}/{sites} directory where all of the website folders are located. |
sslPaths |
object | Optional. Default: { key: '', cert: '', ca: [] } . If "sslPaths" is given, then sites are loaded on https at 443 instead of http on port 80 . |
viewDirs |
string[] | Optional. viewEngine is required. An array of directories where view files are located. It calls app.set('views', viweDirs) . |
viewEngine |
string | Optional. An Express view engine. It calls app.set('view engine', viewEngine) . See Using template engines with Express. |
start
Starts the server.
Param | Type | Description |
---|---|---|
message |
string | Optional. A message to display when the server starts. |
suppressLog |
boolean | Optional. Default: false . Silently starts the server. |
stop
Stops the server.