Lux
A MVC style framework for building highly performant, large scale JSON APIs that anybody who knows the JavaScript language and its modern features will understand.
* Inspired by Rails, Ember, and React.
Disclaimer:
This isn't another wrapper around Express or a framework for building frameworks. This also isn't a replacement for server-side frameworks that render DHTML.
Check out the Medium Article!
What?
Features
- Automatic CRUD actions in controllers
- Automatic pagination, sorting, filtering via query params in controllers
- CLI for eliminating boiler plate
- JSON API 1.0 compliant out of the box
- Chunked/Streamed JSON responses to client over http
- Optimized database queries based on serialized attributes and associations
- Highly extensible - just write reusable JavaScript functions
- Pairs nicely with client-side JavaScript applications 🍷
- Easy to contribute
- Routes are stored and accessed via a
Map
not an Array
- Embraces ES2015 and beyond
- Classes
- Modules
- Promises & async/await
- Arrow Functions
- etc.
Philosophies
Minimal API surface area
Lux utilizes JavaScript's standard library rather than creating a ton of functions you'll have to learn and remember.
After your learn how to use it, you'll rarely need to look at the docs.
Pure functions are awesome
Or more appropriately somewhat pure functions are awesome.
Serving content is done by returning objects, arrays, or other primitives rather than calling res.end(/* content */);
and returning nothing.
Convention over configuration
Rails and Ember are great because they make hard decisions for you and make it possible to submit a PR on your first day at a new company. This is rare with Node server frameworks.
Why?
Frameworks like Rails are pretty great. You can build amazing applications in a reasonable amount of time without a ton of developers working on a project. They have their limitations though. They can be slow and sometimes hard to scale. Not to mention WebSocket support being so-so.
Node to the rescue.
It's fast, it allows the developer to get low level with a relatively simple API, WebSockets are stable and supported out of the box, and last but not least it's just JavaScript.
Not so fast (metaphorically speaking).
The last bit there "It's just JavaScript" has actually been somewhat of a double edge sword. This has positioned Node as a "great prototyping tool" or "only used for micro services".
I can somewhat see why people would think that when returning a list of the first 10 records from a SQL database table looks like this:
app.get('/posts', (req, res) => {
Post.findAll()
.then(posts => {
res.status(200).json(posts);
}, err => {
console.error(err);
res.status(500).send(err.message);
});
});
Could you imagine how ugly that would be if you have implement pagination, filtering, sorting, or better yet formatting the response for JSON API?
Also, where does that code live? What file in what folder would I be able to find that? What pattern do you use for organizing this code?
😲Ok ok give me back Rails I'll worry about performance and scaling later. After all premature optimization is the root of all evil.
Problem.resolve();
Shouldn't there be a better way to do this? Can't I just return a promise or a JavaScript primitive instead of basically using the native Node http server API?
Fortunately ES2015+ has introduced great new features to the JavaScript language, especially when it comes to meta programming.
With Lux your code from before can now look like this:
class PostsController extends Controller {
index(req, res) {
return Post.all();
}
}
Except CRUD actions are taken care of automatically so it would actually look like this:
class PostsController extends Controller {
}
It's about time a Node server framework learned something from client-side JS frameworks.
How?
Installation
npm install -g lux-framework
Creating Your First Project
Use the new
command to create your first project.
lux new <app-name>
Running
To run your application use the serve
command.
cd <app-name>
lux serve
For more information checkout out the Guides.
Benchmarks
https://github.com/postlight/lux-benchmarks
Contributing
Installation
git clone https://github.com/postlight/lux
cd lux
npm install
Testing
git clone https://github.com/postlight/lux
cd lux
npm install
cat test/fixtures/data.sql | mysql -u root -p
npm test
Useful Links
1.0.0-rc (June 25, 2016)
🔅🎊🎈 This is the final set of functionality that will be added in 1.0! The remainder of pull requests from now until the 1.0 release will just be bug fixes or adding polish (Dockerfile, Website, Quick Start Guide, API docs, etc.). These issues can be tracked in the 1.0 milestone.
In addition to features, this release includes bug fixes and general performance improvements.
Features
Lux Console
You can now debug your application rails style with a custom repl that has your application built and loaded as global variables.
To start the repl, run lux console
or lux c
in your
> User.find(1).then(user => {
console.log(`${user.name} is working as expected.`);
});
// => Promise
// 'Stephen Curry is working as expected.'
> PostsController.beforeAction
// => [[Function], [Function], [Function]]
> routes
// => Router {...routes}
Intelligent Responses
Lux now intelligently observes the return value (or resolved Promise
value for async functions) of your applications middleware and controller actions to serialize and respond with the correct data and status codes. Throwing an error at any point in time will cause a 500
and will be caught and handled gracefully (stack traces included when running outside of production).
These are a few example edge cases where returning a Model or an Array of Models may not be what you want to do.
import { Controller } from 'lux-framework';
class ApplicationController extends Controller {
beforeAction = [
/**
* If any request is sent to this application with `?bad=true` the server
* will respond with 400 (Bad Request) and the latter actions will not be
* called. Otherwise, the request will be handled normally.
*/
function isGood(req) {
if (req.params.bad) {
return 400;
}
}
];
/**
* This will return 204 (No Content) and is equivalent to `return 204`.
*/
health() {
return true;
}
/**
* This will return 401 (Unauthorized) and is equivalent to `return 204`.
*/
topSecret() {
return false;
}
/**
* This will return 200 (OK) with the string 'bar'.
*/
foo() {
return 'bar';
}
/**
* This will return 200 (OK) with the following JSON.
*
* {
* "foo": "bar"
* }
*/
fooJSON() {
return {
foo: 'bar'
};
}
/**
* This will return 404 (Not Found). Returning undefined will also result in
* a 404 unless the function returning undefined is called from beforeAction.
*/
notFound() {
return null;
}
}
export default ApplicationController;
Windows Support
Lux now is 100% compatible with Windows!
NOTE:
Travis-CI does not enable us to run our test suite on Windows. This shouldn't be an issue for development but it is highly recommend that you run Lux in a Docker container if your a deploying to Windows in production.
Notable Changes
-
lux serve
does not start in cluster mode by default. To run your application as a cluster run lux serve -c
or lux serve --cluster
.
-
Commands that require an application build (serve
, db:*
, etc) now prefer strict mode and require you to specify --use-weak
if you do not want to run in strict mode (you should pretty much always use strict mode).
Upgrading
The Lux CLI in 1.0 is not backwards compatible with previous beta versions so please perform a local upgrade before upgrading Lux globally.
Routes
Route definitions now must call this.route
and this.resource
rather than having route
and resource
as arguments to the function in ./app/routes.js
. This is the initial ground work for implementing router namespaces.
// ./app/routes.js
export default function routes() {
this.resource('post');
this.resource('users');
this.route('users/login', {
action: 'login',
method: 'POST'
});
this.route('users/logout', {
action: 'logout',
method: 'DELETE'
});
}
Commits
- [
81f52fc1c8
] - feat: add luxify function for converting traditional middleware (#183) (Zachary Golba) - [
39ce152574
] - fix: index names sometimes exceed max length in generated migrations (#182) (Zachary Golba) - [
fb5a71a897
] - feat: add custom repl for debugging (#180) (Zachary Golba) - [
c2b0b30d01
] - feat: do not cluster by default use -c || --cluster (#179) (Zachary Golba) - [
785ebf1c39
] - fix: regression from #177 local lux not being used in cli (#178) (Zachary Golba) - [
67b9940e5c
] - feat: add windows support (#177) (Zachary Golba) - [
c4ab5e0b3b
] - deps: update rollup to version 0.33.0 (#176) (Greenkeeper) - [
a7e860dd97
] - deps: update rollup-plugin-babel to version 2.6.1 (#172) (Greenkeeper) - [
68e7d8fafe
] - deps: update rollup-plugin-json to version 2.0.1 (#173) (Greenkeeper) - [
7abf664c99
] - deps: update rollup-plugin-eslint to version 2.0.2 (#175) (Greenkeeper) - [
f4e17aabf9
] - deps: update rollup-plugin-node-resolve to version 1.7.1 (#174) (Greenkeeper) - [
c2a77c68d5
] - deps: update rollup to version 0.32.4 (#171) (Greenkeeper) - [
b23873109b
] - deps: update rollup-plugin-babel to version 2.6.0 (#169) (Greenkeeper) - [
7ed59ab595
] - deps: update rollup to version 0.32.2 (#168) (Greenkeeper) - [
b25237a647
] - refactor: use process.cwd() instead of process.env.PWD (#167) (Zachary Golba) - [
5bab51a38b
] - deps: update babel-eslint to version 6.1.0 (#165) (Greenkeeper) - [
53cb1e53e2
] - deps: update rollup to version 0.32.1 (#164) (Greenkeeper) - [
022b2e954c
] - deps: upgrade pg version in test-app (#166) (Zachary Golba) - [
ef3f9ce8d3
] - fix: ensure lux is not an external dependency (#163) (Zachary Golba) - [
fba8654d2e
] - deps: update test-app dependencies (#162) (Zachary Golba) - [
84160c9149
] - deps: update babel-core to version 6.10.4 (#161) (Greenkeeper) - [
7ee935afa1
] - deps: update babel-eslint to version 6.0.5 (#160) (Greenkeeper) - [
cd53552aca
] - deps: update eslint to version 2.13.1 (#159) (Greenkeeper) - [
8e6a23dad3
] - refactor: improve build process and stack traces (#158) (Zachary Golba) - [
6748638ca6
] - refactor: rename serializer methods and return objects (#155) (Zachary Golba) - [
7597031076
] - feat: ensure Application#port is a number (#156) (Zachary Golba) - [
2d83f30df6
] - deps: update rollup to version 0.32.0 (#154) (Greenkeeper) - [
64250ebe5b
] - refactor: separate responsibilities in req/res flow (#153) (Zachary Golba)