Skip to content
Grunt plugin which uses a json server to generate REST routes from a json / js file
JavaScript
Branch: master
Clone or download
Mitko Tschimev
Mitko Tschimev Merge pull request #13 from ivandack/master
Added support for readOnly, noGzip, noCors and serving static content.
Latest commit 6e18ab2 Dec 2, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tasks
test/fixtures
.gitignore
.jshintrc
Gruntfile.js
LICENSE-MIT
README.md
package.json

README.md

grunt-json-server

Give it a JSON or JS seed file and it will serve it through REST routes.

Getting Started

This plugin uses JSON Server to serve a REST api from a provided db file. For more information please go to JSON Server.

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-json-server --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-json-server');

The "json_server" task

Overview

In your project's Gruntfile, add a section named json_server to the data object passed into grunt.initConfig().

grunt.initConfig({
  json_server: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
});

Options

options.port

Type: Number Default value: 13337

A number value that is used for the server port.

options.hostname

Type: String Default value: '0.0.0.0'

A string value that is used for the server address.

options.db

Type: String Default value: 'db.json'

A string value that is the location to the db file which gets translated to restful routes

options.routes

Type: String Default value: undefined

A string value that is the location to the routes JSON file which contains additional routes

options.customRoutes

Type: Object Default value: undefined

A key-value pairs of custom routes that should be applied to server.

options.readOnly

Type: Boolean Default value: false

Allow only GET requests. Any other method returns 403 (Forbidden).

options.noGzip

Type: Boolean Default value: false

Disable GZIP Content-Encoding.

options.noCors

Type: Boolean Default value: false

Disable Cross-Origin Resource Sharing.

options.static

Type: String Default value: undefined

Set static files directory.

Usage Examples

Default Options

In this example, the file db.json in the api folder gets parsed and translated to restful routes and starts a server on http://0.0.0.0:13337

grunt.initConfig({
    json_server: {
        options: {
            port: 13337,
            hostname: '0.0.0.0',
            db: 'api/db.json'
        }
    },
});

Custom routes

You can pass an object with configuration for custom routes

grunt.initConfig({
     json_server: {
         options: {
             port: 13337,
             hostname: '0.0.0.0',
             db: 'api/db.json',
             customRoutes: {
                 '/big_post': {
                    method: 'get',
                    handler: function(req, res) {
                        return res.json({id: 1, title: 'Big post'});
                    }
                 }
             }        
         }
     }
});

Usage with connect in grunt

If you want to use it with the grunt connect plugin it is recommend to setup it with the concurrent plugin otherwise json_server will block everything

grunt.initConfig({
    json_server: {
        options: {
            port: 13337,
            hostname: '0.0.0.0',
            db: 'api/db.json'
        }
    },

    // Run some tasks in parallel to speed up build process
    concurrent: {
        server: {
            tasks: [
                'json_server',
                'watch'
            ],
            options: {
                logConcurrentOutput: true
            }
        }
    }
});

grunt.task.run([
    'connect:livereload',
    'concurrent:server'
]);

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

MIT License

Author

Mitko Tschimev

You can’t perform that action at this time.