0.3.3 • Published 2 years ago

@sempervirens/server v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.

Tests badge Version badge

Installation

npm i @sempervirens/server

Usage

  1. 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
  1. Implement the Server class in the server.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
});
  1. Run node server.
cd {root}
node server

API

constructor

ParamTypeDescription
corsOptionsobjectOptional. Default: {}. See NPM cors package configuration options.
httpRedirectServerPortnumberOptiona. Default 80. Sets the HTTP server port for the HTTP to HTTPS redirect functionality. Primarily for testing.
enableRedirectToHttpsbooleanOptional. 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.
enableRedirectToWwwbooleanOptional. Default: false. If true, then when a request arrives at "http(s)://{domain}", it is redirected to "http(s)://www.{domain}".
middlewareobject[]Optional. Default: []. Shape: { path?: string; handler: RequestHandler|function; }. Adds server-level middleware to requests for all sites before the endpoints are called. If "path" is given, then it only adds the middleware to specified path. Otherwise, it adds the middleware to all paths.
portnumberOptional. 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.
sitesobjectRequired. 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.)
sitesDirstringOptional. Default: sites. Sets the name of the {root}/{sites} directory where all of the website folders are located.
sslPathsobjectOptional. Default: { key: '', cert: '', ca: [] }. If "sslPaths" is given, then sites are loaded on https at 443 instead of http on port 80.
viewDirsstring[]Optional. viewEngine is required. An array of directories where view files are located. It calls app.set('views', viweDirs).
viewEnginestringOptional. An Express view engine. It calls app.set('view engine', viewEngine). See Using template engines with Express.

start

Starts the server.

ParamTypeDescription
messagestringOptional. A message to display when the server starts.
suppressLogbooleanOptional. Default: false. Silently starts the server.

stop

Stops the server.

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago