Lighter is a lightweight Node.js framework for web applications and APIs.
When used for web applications, Lighter will render views either on the server or the client. This functionality will allow Lighter to optimize for low latency. The first request to your web app renders HTML and returns it to the browser, then views are lazy-loaded into the client so that subsequent requests only need to return JSON that the client can render using its cached views.
Lighter is influenced by the PHP framework CodeIgniter, in that by default, controllers and their methods will automagically get added to the router based on their names and where they are found under the controllers directory. This means you don't have to write a ton of routing code, and you know where to find a controller method if you know its URL, and vice versa.
Lighter's default router is extremely simple so that it can respond faster than other
frameworks such as Express, Restify and Meteor. The caveat is that dynamic paths are
not supported by default, so you would need to do /blog/article?id=1
rather than
/blog/article/1
. It's a small price to pay for amazing server throughput.
Lighter's simplicity makes it great for APIs, and we'll be adding functionality to auto-generate API documentation.
To use lighter, first you must install it.
npm i lighter
Using lighter is as simple as creating a lighter directory structure (as seen below)
and then creating an app.js
that requires lighter.
var lighter = require('lighter');
Then you can run your app with Node.
node app
Soon, this setup process will be replaced with a CLI that will go something like:
sudo npm i -g lighter
lighter new myapp
cd myapp
node app
Lighter uses Chug to cache its static assets, and each type of asset comes from an assigned directory. Chug also loads models views and controllers. Changes inside those directories will trigger reloads of the items that have been changed.
/
controllers/
models/
public/
scripts/
styles/
views/
If you want to load assets from other directories (such as node_modules/), you can
add to the list of locations using an API method like addPublics
, addScripts
,
addStyles
or addViews
.
Here's a sample controller that you could save into
/controllers/ContactController.js
:
var Controller = require('lighter/lib/Controller');
module.exports = Controller.extend({
index: function GET(request, response) {
response.writeHead(200, {'content-type': 'text/html'});
response.end('TODO: Show contact info and a form.');
},
send: function POST(request, response) {
response.writeHead(200, {'content-type': 'text/html'});
response.end('TODO: Thank the user for contacting us.');
}
});
To see the contact us form, you would visit /contact
, and the form you submit
would post to /contact/send
. Once we have models, we can do more interesting
things here.
Models are coming soon...
Views are rendered by name from response.view
.
The following would render a template that is stored at /views/hello.ltl
with a
context indicating who to say hello to.
function GET(request, response) {
response.view('hello', {who: 'World'});
}
The location
argument can be a single location or an array of locations. Lighter
adds this location to the array of controller locations to be chugged.
The location
argument can be a single location or an array of locations. Lighter
adds this location to the array of public file locations to be chugged.
The location
argument can be a single location or an array of locations. Lighter
adds this location to the array of script file locations to be chugged.
The location
argument can be a single location or an array of locations. Lighter
adds this location to the array of style file locations to be chugged.
The location
argument can be a single location or an array of locations. Lighter
adds this location to the array of view file locations to be chugged.
If you would like to use Express (or another framework that exposes
app.get(path, callback)
etc., to do your routing, you can set Lighter to do this.
Set an alternative logger that exposes logger.log(message)
.
Set the port that you would like Lighter to use for HTTP.
Set the port that you would like Lighter to use for HTTPS.
Set an array of lines that you would like to be displayed upon startup.
A reference to the Chug module that Lighter uses.
A reference to the Beams module that Lighter uses.
A reference to the Colors module that Lighter uses.
A reference to the Express-like app that's been set via lighter.setApp(app)
.
A reference to the logger that's been set via lighter.setLogger(logger)
.
The files that have been added via lighter.setPublic(location)
or
found in the publics/
directory.
The scripts that have been added via lighter.setPublic(location)
or
found in the scripts/
directory.
The stylesheets that have been added via lighter.setPublic(location)
or
found in the styles/
directory.
The views that have been added via lighter.setPublic(location)
or
found in the views/
directory.
The controllers that have been found in the controllers/
directory.
The models that have been found in the models/
directory.
The environment string that comes from process.env.NODE_ENV. Expected values
are dev
, test
, stage
, canary
or prod
.