Class

WebServer

WebServer(optionsopt) → {WebServer}

Create a WebServer.

Constructor

# new WebServer(optionsopt) → {WebServer}

Parameters:
Name Type Attributes Description
options WebServer~WebServerOptions <optional>

WebServer Options.

View Source WebServer.js, line 80

WebServer
Examples

Simple example

const { WebServer } = require("lite-web-server");
var server = new WebServer();

server.start();

Options Example

const { WebServer } = require("lite-web-server");
var server = new WebServer({
  port: 3000, //port
  directory: "./public", //directory to publish
  serveindex: false, //disable serve-Index feature.
  acceptonlyget: true, //returns 405 to the request with method other than GET.
  useindexhtml: true, //returns /index.html on that directory when the directory was requested.
  rootfile: "/index.html", //the file that is used as the home page.
  errordocument: {
    _404: `./public/404.html`, //File path to use as a 404 error page.
    _405: `./public/405.html`, //File path to use as a 405 error page.
  }
});

Methods

# start() → {Promise.<(Object|null)>}

View Source WebServer.js, line 117

Promise.<(Object|null)>

Type Definitions

object

# WebServerOptions

WebServer's options.

Properties:
Name Type Attributes Default Description
options object <optional>
port Number <optional>
3000

Port to host the page.

directory string <optional>
./public/

The directory to load the file.

serveindex boolean <optional>
false

If it's true, return the file list of the directory when the directory was requested. (If index.html exists in the directory, it will be sent preferentially)

acceptonlyget boolean <optional>
true

Only accepts get request.

useindexhtml boolean <optional>
true

If it's true, returns ./index.html file when requested directory.

rootfile string <optional>
/index.html

You can specify a file to load as a top page. (Please specify files in the published directory.)

errordocument object <optional>
errordocument._404 string <optional>
`${__dirname}/assets/def_pages/404.html`

Html file path to respond on the request methods other than GET.

errordocument._405 string <optional>
`${__dirname}/assets/def_pages/405.html`

Html file path to respond on not found.

View Source WebServer.js, line 24

Example

All Options Example

const { WebServer } = require("lite-web-server");
var server = new WebServer({
  port: 3000, //port
  directory: "./public", //directory to publish
  serveindex: false, //disable serve-Index feature.
  acceptonlyget: true, //returns 405 to the request with method other than GET.
  useindexhtml: true, //returns /index.html on that directory when the directory was requested.
  rootfile: "/index.html", //the file that is used as the home page.
  errordocument: {
    _404: `./public/404.html`, //File path to use as a 404 error page.
    _405: `./public/405.html`, //File path to use as a 405 error page.
  }
});
object

# WebServerRequestLog

Properties:
Name Type Description
options object
method Number

The method that used on request.

url string

Requested URL.

requestedAt object

Requested time object.

requestedAtTimestamp Number

Requested time in timestamp.

raw string

The string can be used for the output directly.

View Source WebServer.js, line 389

Events

object

# debug

Debug messages.

Properties:
Type Description
string

Debug log message.

View Source WebServer.js, line 369

Example
server.on("debug", msg => console.log(msg))

object

# requestlog

WebServer request event for logging.

Properties:

View Source WebServer.js, line 379

Example
server.on("requestlog", requestlog => console.log(requestlog.raw))