Expo is a meta-framework for rapid Node.js web development. It provides you will the sensible defaults you need to build the backend of a modern web application.
# Install it via:
$ npm install -g expo
$ expo --help
- Routing
- Assets
- Asset pipeline -- via connect-assets
- Concatenation, compression, cache-busting
- Support for CoffeeScript, Less, Stylus
- Database
- Database ORM -- via Sequelize
- Database migrations
- Testing
- Tests -- via Mocha
- Assertions -- via Chai
- Integration tests -- via superagent
- Utilities
- Command-line tasks -- via commander.js
- Auto-restarting on changes -- via supervisor
- Logging
- Config file loading
Expo lets you start up sites very fast.
Create a project using the expo
command. Here we'll use --bare
, which
generates the bare minimum:
$ expo --bare hello
hello/ your project
Readme.md
run
app.js
.gitignore
package.json
initializers/ code that initializes the app environment
app/initializers/app.js
routes/ auto-loaded files that create URL routes
app/routes/home.js
views/ template files
app/views/index.jade
app/views/layout.jade
install dependencies:
$ cd hello
$ npm install && npm shrinkwrap
run the app:
$ ./run server
more info:
$ ./run --help
Read about creating projects >
...But this isn't always that useful, since you probably will need assets,
databases, sessions, tests, helpers and other doodads. Use expo
without
switches to generate the default layout:
$ expo hello
hello/ your project
Readme.md
run
app.js
.gitignore
package.json
assets/ where your images, CSS and JS files live
app/assets/css/application.styl
app/assets/js/application.js
app/assets/img/.gitkeep
initializers/ code that initializes the app environment
app/initializers/app.js
app/initializers/assets.js
app/initializers/sessions.js
helpers/ functions available in your views
app/helpers/app_helper.js
routes/ auto-loaded files that create URL routes
app/routes/home.js
tasks/ command line tasks
app/tasks/db.js
views/ template files
app/views/index.jade
app/views/layout.jade
config/ yaml config files
config/database.yml.example
config/secret_token.yml
db/ development sqlite database
db/.gitkeep
lib/ models and code
lib/.gitkeep
public/ normal files go here
public/robots.txt
test/ tests, specs
test/setup.js
test/app_test.js
install dependencies:
$ cd aoeu
$ npm install && npm shrinkwrap
Read about creating projects >
Expo projects come with a runner that gives you access to default tasks. Simply invoke ./run in a project.
$ cd myproject
$ ./run --help
Usage: run [options] [command]
Commands:
server [port] [..] Starts the server (alias: "s")
console Opens a console (alias: "c")
runner [cmd] Runs a command (alias: "r")
db-create Creates the environment's database
db-drop Drops the environment's database
db-migrate Run database migrations
gen-migration [name] Creates a migration file in migrations/
assets-precompile Precompiles asset files
Options:
-h, --help output usage information
-V, --version output the version number
-e, --env [env] Environment to start in [develompent]
Run your a project using ./run server
(or its shortcut: ./run s
).
In development mode, you server will automatically reload on code changes.
$ ./run s
=> Auto-restarting on changes
=> Running development mode at http://0.0.0.0:4567
=> Ready [290ms]
GET / 200 486ms - 241b
GET / 200 116ms - 241b
GET /css/application.css 200 10ms - 36b
GET /js/application.js 200 2ms - 1b
And you get a convenient REPL console.
$ ./run c
> 2 + 4
6
> app.get('env')
'development'
Your Expo app behaves just like any Node package would. It has a main entry
point, app
, which is a Connect app.
$ node
> app = require('.')
{ ... }
> app.get('env')
'development'
> app.conf('database').username
'youruser'
> http.createServer(app).listen(5678);
Tests are available via Mocha and Chai. Just like all Node packages, the command
is the standard npm test
.
$ npm test
> [email protected] test
> ./node_modules/.bin/mocha -R spec
> App loaded for test environment
App
Homepage should work (799ms)
should have the right env
2 tests complete (807 ms)
Here's the main entry point, and the main initializer.
You can dump JS files in app/initializers/
and they will be automatically
loaded when the app is loaded.
$ cat app.js
var app = module.exports = require('express')();
require('expo')(app, __dirname);
$ cat app/initializers/app.js
var express = require('express');
module.exports = function(app) {
app.set('view engine', 'jade');
app.use(express['static'](app.path('public')));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.configure('development', function() {
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.errorHandler());
});
};
Routes in app/routes are automatically loaded.
$ cat app/routes/home.js
module.exports = function(app) {
app.get('/', function(req, res) {
res.render('index', {});
});
};
These will be available in your views automatically.
$ cat app/helpers/name_helper.js
module.exports = {
hello: function(name) {
return "Hello " + name;
}
};
$ cat app/views/hello.jade
div= hello("Rico")
Expo provides you helpers for Yaml and JSON configuration files via
app.conf()
.
$ cat config/database.yml
production:
username: 'rsc'
dbname: 'foobar_production'
development:
username: 'rsc'
dbname: 'foobar_development'
$ ./run console
> app.conf('database').dbname
"foobar_development"
© 2013, Rico Sta. Cruz. Released under the MIT License.
Image was taken from Flickr, licensed under Creative Commons.
Expo is authored and maintained by Rico Sta. Cruz with help from its contributors.
- My website (ricostacruz.com)
- Github (@rstacruz)
- Twitter (@rstacruz)