Give it a JSON or JS seed file and it will serve it through REST routes.
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-devOnce the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-json-server');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. }, }, });Type: Number Default value: 13337
A number value that is used for the server port.
Type: String Default value: '0.0.0.0'
A string value that is used for the server address.
Type: String Default value: 'db.json'
A string value that is the location to the db file which gets translated to restful routes
Type: String Default value: undefined
A string value that is the location to the routes JSON file which contains additional routes
Type: Object Default value: undefined
A key-value pairs of custom routes that should be applied to server.
Type: Boolean Default value: false
Allow only GET requests. Any other method returns 403 (Forbidden).
Type: Boolean Default value: false
Disable GZIP Content-Encoding.
Type: Boolean Default value: false
Disable Cross-Origin Resource Sharing.
Type: String Default value: undefined
Set static files directory.
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' } }, });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'}); } } } } } });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' ]);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.