A RESTful routing middleware for Express.js.
Assuming you have already created an Express application by following the Express installation instructions. Now:
-
add expRESTive to your package.json file:
$ npm install --save express exprestive
-
add the expRESTive middleware to your application
express = require 'express' exprestive = require 'exprestive' app = express() app.use exprestive() app.listen 3000
-
create a
routes.coffee
file in the same directory as your servermodule.exports = ({ GET, POST, PUT, DELETE }) -> GET '/hello', to: 'helloWorld#index'
-
create a
controllers/
directory in the same directory as your server and populate it with controllers. File names don't matter controller names are taken from the class name# controllers/hello_world.coffee class HelloWorldController index: (req, res) -> res.end 'hello world' module.exports = HelloWorldController
-
visit
/hello
in your browser
Exprestive exports url building functions to a paths
object. By default it is saved to res.locals.paths
for easy access from views.
In your routes file you can pass an as
parameter to non-restful routes to define a reverse route.
# routes.coffee
module.exports = ({GET}) ->
GET '/foo/bar', to: 'foo#bar', as: 'foobar'
In a controller you can access this path with res.locals.paths.foobar()
# controllers/foo.coffee
class FooController
bar: (req, res) ->
res.locals.paths.foobar() # returns "/foo/bar"
In a view you can access this path with paths.foobar()
//- index.jade
a(href=paths.foobar()) Visit foobar
The resources
directive automatically sets reverse routes
# routes.coffee
module.exports = ({resources}) ->
resources 'users'
This sets the following path helpers automatically:
Path helper | Return Value |
---|---|
paths.users() |
/users |
paths.user(123) |
/users/123 |
paths.newUser() |
/users/new |
paths.editUser(123) |
/users/123/edit |
Setting options.paths
will cause reverse routes to be exported to the passed object instead of res.locals.paths
For example you can set options.paths
to a global variable to access paths the same from everywhere.
global.paths = {}
app.use exprestive paths: global.paths
# Now paths.foobar() returns "/foo/bar" from anywhere in your application
Options are provided to the exprestive function.
app = express()
app.use exprestive
appDir: './www'
Option | Description | Default Value |
---|---|---|
appDir | Directory used as a base directory for routes file and controllers directory | __dirname |
routesFilePath | File to be required to define routes. This is passed to require , so extension is optional |
appDir + /routes |
controllersDirPath | Directory in which to look for controllers. All files in this directory will be automatically required | appDir + /controllers |
routes | Pass in a routes function instead of requiring routesFilePath. Setting this will cause routesFilePath to be ignored | None |
controllers | Pass in an object of instantiated controllers instead of requiring controller classes from controllersDirPath. Setting this will cause controllersDirPath to be ignored | None |
paths | Pass in an object to export paths to (see reverse routing) | res.locals.paths |
- Run unit tests:
npm run unit-tests
- Run feature tests:
npm run feature-tests