Getting Started

caution

Light is currently in development! Please note there may be breaking changes in between minor and beta versions! We will adhere to semantic versioning starting with v2.0.0

Getting started is quick and easy! This guide assumes you are running the latest LTS version of node. There is also a live example at the bottom if you want to try out light without setting it up locally!

Install

Initialize a new repository with npm init. Then install light.

npm install light

Then add the following scripts to your package.json.

{
// ...
"scripts": {
"start": "light start",
"dev": "light dev"
}
// ...
}

Usage

Once you have installed light, all .ts and .js files under the routes directory will automatically be imported. Typescript will be automatically run/compiled if there is a tsconfig.json in the root directory. Additionally, all files that end in .test.(t|j)s and files in __tests__ folders will be ignore.

To get started, lets populate routes/index.js. Please note we use Next.js style routing. You can check out the routing docs on the left for more information.

const { createRoute } = require('light');
const { route, GET } = createRoute();
GET(() => {
return {
hello: 'world',
};
});
module.exports = route;

This is probably different that what you are used to with express, koa, hapi, etc. There are a few interesting parts to this file. The createRoute function is the core of light and it returns an object containing ROUTE, GET, POST, PUT, PATCH, DELETE, and a bunch of other HTTP methods. For each route you can add whatever methods you like to support. Any methods you do not define will return 404 Not Found. At the bottom we export route. route is an object containing all of the information needed to execute your code. If the server is started with the CLI, it will transform that object and inject it in the router, but if it is run in a serverless environment, it will transform it into a request handler.

Once you are done editing the file, run npm run dev to start the server with hot module reloading. If you did everything right, your terminal should output something similar to this.

[ start ] 🔥 igniting the server 🔥
[ listening ] on port 3000
[ hmr ] starting the hot reloader
[ hmr ] watching for changes

Conclusion

Phew, that was a lot. Don't worry though, you just learnt about 80% of light! It's really that simple. You now have a dev server that

  • hot swaps any code you change without restarting the server
  • can support many different serverless and hosting platforms
  • has very little magic behind the scenes, what you see in a route is what you get

Feel free to continue with the guides in the sidebar to understand more of the framework!

Live Example

If you want to mess around with the example provided above without setting it up on your computer, you can try it out on RunKit! Just edit the code below and click the URL in the bottom right.