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
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'
}
},
});
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.