Git Product home page Git Product logo

exprestive's Introduction

expRESTive Build Status Dependency Status

A RESTful routing middleware for Express.js.

Basic usage

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 server

    module.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

Reverse routing

Exprestive exports url building functions to a paths object. By default it is saved to res.locals.paths for easy access from views.

Saving / accessing reverse routes

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

Restful routing

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

Custom paths

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

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

Tests

  • Run unit tests: npm run unit-tests
  • Run feature tests: npm run feature-tests

exprestive's People

Contributors

alexdavid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.