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
- 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
cd test/test-app
npm install
cd ../../
npm test
Useful Links
1.0.0-rc.4 (Aug 7, 2016)
This release contains a fix for a number of bugs introduced in 1.0.0-rc.3
. In addition to bug fixes this release introduces a couple new features.
Features
Generating Utilities
You can now generate utility functions via the command line!
$ lux generate util do-something
create app/utils/do-something.js
// app/utils/do-something.js
export default function doSomething() {
}
Generating Middleware
You can now generate middleware functions via the command line!
$ lux generate middleware do-something
create app/middleware/do-something.js
// app/middleware/do-something.js
export default function doSomething(/*request, response*/) {
}
Upgrading
Make sure the following directories exist:
Also, 1.0.0-rc.3
introduced some changes to config files so make sure that the files in your config/environments
directory match the following format:
// config/environments/development.js
export default {
logging: {
level: 'DEBUG',
format: 'text',
enabled: true,
filter: {
params: []
}
}
};
// config/environments/test.js
export default {
logging: {
level: 'WARN',
format: 'text',
enabled: false,
filter: {
params: []
}
}
};
// config/environments/production.js
export default {
logging: {
level: 'INFO',
format: 'json',
enabled: true,
filter: {
params: []
}
}
};
Commits
- [
62431679b2
] - deps: update rollup to version 0.34.7 (#284) (Greenkeeper) - [
0c12e8b754
] - fix: indentation is off when using model generators (#281) (Zachary Golba) - [
4d89474fce
] - refactor: remove bluebird in favor of native apis (#279) (Zachary Golba) - [
241ae8993d
] - refactor: improve third party type declarations (#276) (Zachary Golba) - [
5de7a0d6c3
] - fix: empty n:m relationships loads every record instead of an empty array (#277) (Zachary Golba) - [
48d2fb7377
] - deps: update rollup to version 0.34.5 (#275) (Greenkeeper) - [
6ea44c3aef
] - deps: update babel-core to version 6.13.2 (#273) (Greenkeeper) - [
329309d9ec
] - fix: empty 'fields' params cause all keys of a resource to be exposed (#270) (Zachary Golba) - [
a412aa7493
] - deps: update test-app dependencies (#268) (Zachary Golba) - [
35f301c1d8
] - deps: update babel-core to version 6.13.1 (#267) (Greenkeeper) - [
d0f4f2bfd3
] - Feat: Added generator for utils (#262) (Adam Pash) - [
2ead12a73a
] - deps: update mocha to version 3.0.1 (#265) (Greenkeeper) - [
94abf145d1
] - Feat: Added generator for middleware (#261) (Adam Pash) - [
d9d008111c
] - release: 1.0.0-rc.3 (#264) (Zachary Golba)