lite-server is a free, open source frameworks & platforms project written in JavaScript and released under MIT. It has 2,306 GitHub stars, 261 forks and 79 open issues, and was last pushed 3 months ago. On this registry it ranks #65 of 87 tracked projects in Frameworks & Platforms, with 5 head-to-head comparisons available.

What is lite-server?

lite-server is a lightweight, development-only Node.js server for developers building single-page applications who need HTML5 route fallback and live browser refresh out of the box.

What it is

lite-server is a customized wrapper around BrowserSync, published to npm as lite-server and licensed under MIT. It serves a web app's static content, opens it in the default browser, refreshes the page when HTML or JavaScript files change, injects CSS changes over sockets, and returns a fallback page when a requested route is not found. It runs only during development; it is not a production server.

The concrete problem it solves concerns deep links in single-page applications. A route such as /customer/21 may be a client-side route known only to an Angular app, so when that URL is entered manually or linked to directly, the request reaches the static server before the framework has loaded, and the server returns a 404 because no file matches. BrowserSync, which already provides fast static serving and change detection, does not automatically allow a fallback page, though it does allow custom middleware; lite-server adds that HTML5 route fallback to ./index.html by default and makes it easy to configure, filling the gap between BrowserSync and SPA development in the Node.js/npm ecosystem.

Key capabilities

  • Serves static content from a configurable base directory, defaulting to the current folder, with --baseDir= available as a run-time option.
  • Detects changes to HTML or JavaScript and refreshes the browser, while injecting CSS changes using sockets.
  • Applies an HTML5 route fallback to ./index.html by default, returning the starting page instead of a 404 for unmatched routes.
  • Accepts configuration overrides through a local bs-config.json or bs-config.js file, with the path supplied via -c or --config=.
  • Exposes a bs-config.js file that may export an object or a function receiving the lite-server BrowserSync instance as its only argument.
  • Allows server middleware to be replaced, for example overriding the second middleware with require('connect-history-api-fallback')({ index: '/index.html', verbose: true }).
  • Supports configuration of the server port, watched file paths such as ./src/**/*.{html,htm,css,js}, and the server.baseDir.

Who uses it and how

  • Single-page application developers who need deep links like /customer/21 to load the app shell rather than a 404 while Angular or another framework has not yet run.
  • Front-end developers who add a "dev": "lite-server" script to package.json and start the server with npm run dev.
  • Developers working on the fly who run npx lite-server without installing anything permanently, or who install it globally with npm install --global lite-server.
  • Teams that point the server at a build output directory using --baseDir="dist" and watch a specific source folder via the files setting in bs-config.json.
  • Projects that customize routing behavior by supplying a bs-config.js with a connect-history-api-fallback middleware configured with index: '/index.html' and verbose: true.

Getting started

Install it as a development dependency with npm install lite-server --save-dev (or yarn add lite-server --dev), add a "dev": "lite-server" script to package.json, and start it with npm run dev, or run npx lite-server directly.

How it compares

lite-server sits on top of BrowserSync rather than replacing it: BrowserSync supplies the static serving, change detection, browser refresh, and customizations, while lite-server adds the SPA route-fallback behaviour that BrowserSync does not provide automatically. It is therefore a thin configuration layer for developers already comfortable with BrowserSync's middleware model, not a general-purpose application framework.

When to use it — and when not

Choose it for local development of a single-page application where you want a dev server with route fallback and no production ambitions; a self-hoster should note that it is explicitly development-only and is not meant to serve traffic in production. Developers who need a production-grade server, or who find the README's configuration surface built on BrowserSync's options unfamiliar, should consider alternatives, and the 79 open issues suggest some rough edges remain for particular setups.

project readme (upstream, from github) — read inline

lite-server

Lightweight development only node server that serves a web app, opens it in the browser, refreshes when html or javascript change, injects CSS changes using sockets, and has a fallback page when a route is not found.

AI-Ready Dependency Status npm version Build Status Greenkeeper badge Build Status

Why

BrowserSync does most of what we want in a super fast lightweight development server. It serves the static content, detects changes, refreshes the browser, and offers many customizations.

When creating a SPA there are routes that are only known to the browser. For example, /customer/21 may be a client side route for an Angular app. If this route is entered manually or linked to directly as the entry point of the Angular app (aka a deep link) the static server will receive the request, because Angular is not loaded yet. The server will not find a match for the route and thus return a 404. The desired behavior in this case is to return the index.html (or whatever starting page of the app we have defined). BrowserSync does not automatically allow for a fallback page. But it does allow for custom middleware. This is where lite-server steps in.

lite-server is a simple customized wrapper around BrowserSync to make it easy to serve SPAs.

Installation and Usage

The recommended installation method is a local NPM install for your project:

npm install lite-server --save-dev
yarn add lite-server --dev # or yarn

...and add a "script" entry within your project's package.json file:

# Inside package.json...
  "scripts": {
    "dev": "lite-server"
  },

With the above script entry, you can then start lite-server via:

npm run dev

Other options for running locally installed NPM binaries is discussed in this Stack Overflow question: How to use package installed locally in node_modules

Using on the fly

lite-server can be used with npx

npx lite-server

Global Installation

lite-server can be also installed globally, if preferred:

npm install --global lite-server

# To run:
lite-server

Custom Configuration

The default behavior serves from the current folder, opens a browser, and applies a HTML5 route fallback to ./index.html.

lite-server uses BrowserSync, and allows for configuration overrides via a local bs-config.json or bs-config.js file in your project.

You can provide custom path to your config file via -c or --config= run time options:

lite-server -c configs/my-bs-config.js

For example, to change the server port, watched file paths, and base directory for your project, create a bs-config.json in your project's folder:

{
  "port": 8000,
  "files": ["./src/**/*.{html,htm,css,js}"],
  "server": { "baseDir": "./src" }
}

You can also provide custom path to your base directory --baseDir= run time options:

lite-server --baseDir="dist"

A more complicated example with modifications to the server middleware can be done with a bs-config.js file, which requires the module.exports = { ... }; syntax:

module.exports = {
  server: {
    middleware: {
      // overrides the second middleware default with new settings
      1: require('connect-history-api-fallback')({
        index: '/index.html',
        verbose: true,
      }),
    },
  },
};

The bs-config.js file may also export a function that receives the lite-server Browsersync instance as its only argument. While not required, the return value of this function will be used to extend the default lite-server configuration.

module.exports = function (bs) {
  return {
    server: {
      middleware: {
        // overrides the second middleware default with new settings
        1: require('connect-history-api-fallback')({
          index: '/index.html',
          verbose: true,
        }),
      },
    },
  };
};

NOTE: Keep in mind that when using middleware overrides the specific middleware module must be installed in your project. For the above example, you'll need to do:

npm install connect-history-api-fallback --save-dev

...otherwise you'll get an error similar to:

Error: Cannot find module 'connect-history-api-fallback'

Another example: To remove one of the default middlewares, such as connect-logger, you can set it's array index to null:

module.exports = {
  server: {
    middleware: {
      0: null, // removes default `connect-logger` middleware
    },
  },
};

A list of the entire set of BrowserSync options can be found in its docs:

Testing

When using lite-server to run end to end tests, we may not want to log verbosely. We may also want to prevent the browser from opening. These options in the bs-config.js will silence all logging from lite-server:

  open: false
  logLevel: "silent",
  server: {
    middleware: {
      0: null
    }
  }

Known Issues

CSS with Angular 2 is embedded thus even though BrowserSync detects the file change to CSS, it does not inject the file via sockets. As a workaround, injectChanges defaults to false.

Contributing

  1. Fork and clone it
  2. Install dependencies: npm install
  3. Create a feature branch: git checkout -b new-feature
  4. Commit changes: git commit -am 'Added a feature'
  5. Run static code analysis and unit tests: npm test
  6. Push to the remote branch: git push origin new-feature
  7. Create a new Pull Request

License

Code released under the MIT license.

Frequently asked questions

Is lite-server free to use?

lite-server is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does lite-server do?

Lightweight node server

What is lite-server written in?

lite-server is primarily written in JavaScript. Its source is publicly available at https://github.com/johnpapa/lite-server, and it has 2,306 GitHub stars.