Giter VIP home page Giter VIP logo

gulp-json-server's Introduction

gulp-json-server Build Status

Wrapper for json-server.

Install

$ npm install --save-dev gulp-json-srv

Usage

Server with default parameters

var gulp = require('gulp');
var jsonServer = require('gulp-json-srv');

gulp.task('default', function () {
	jsonServer.start(); // start serving 'db.json' on port 3000
});

Server with custom parameters

var gulp = require('gulp');
var jsonServer = require('gulp-json-srv');

gulp.task('default', function () {
	jsonServer.start({
		data: 'some-else-data-file.json',
		port: 25000,
		id:   '_id',
		baseUrl: '/api', // change base URl from / to /api
		rewriteRules: {
			'/': '/api/',
			'/blog/:resource/:id/show': '/api/:resource/:id'
		},
        customRoutes: {
            '/big_post': {
                method: 'get',
                handler: function(req, res) {
                    return res.json({id: 1, title: 'Big post'});
                }
            }
        },
		deferredStart: true,
		static: './static' // serves HTML, JS and CSS from 'static' folder 
	});
});

Server with DB reloading

var gulp = require('gulp');
var jsonServer = require('gulp-json-srv');
var server = jsonServer.start({
    data: 'db.json',
    deferredStart: true
});

gulp.task('serverStart', function () {
    server.start();
});

gulp.task('watch', function () {
    gulp.watch(['db.json'], function(){
      server.reload();
    });
});

gulp.task('default', ['serverStart', 'watch']);

Server with in-memory DB

var gulp = require('gulp');
var jsonServer = require('gulp-json-srv');

var db = {
	users: [
		{id: 0, name: "user0"},
	],
	posts: [
		{id: 0, title: "title 1", author_id: 0},
	]
};

gulp.task('default', function () {
	jsonServer.start({
		data: db
	});
});

jsonServer API

start(options)

Creates new server with specified options and immediately starts it until deferredStart option specified.

Returns

Returns a wrapper object for the server (see it's API below).

Options

data

Type: string or object
Default: 'db.json'

Input source for server's DB. May be either a path to the file or in-memory object.

port

Type: integer
Default: 3000

Port number on which json-server will listen.

baseUrl

Type: string
Default: null

The base URL for REST API.

rewriteRules

Type: object
Default: null

A key-value pairs of rewrite rules that should be applied to server.

customRoutes

Type: object
Default: null

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

id

Type: string
Default: id

id key used to match objects in collections. Usually id, but for example MongoDB use _id.

deferredStart

Type: bool
Default: false

Used to specify that server object should be created, but not started, assuming manual start later.

static

Type: string
Default: null

If specified and not null, sets the static files folder and lets json-server serve static files from that folder.

Server wrapper object API

start()

Manually starts server in case of deferred start. Has no effect in case server is already running.

kill()

Stops the server.

reload(data)

Reloads server DB with new data. The data can be object or data file path, or can be omitted.

Options

data

Type: string or object
Default: undefined

Input source for new DB's content. May be either a path to the file or in-memory object.

If path to file, or object is passed, reloads DB with new data, no matter if serving file or in-memory DB. If omitted, do nothing in case of in-memory DB, and reload data from file, specified by server options in case of serving the file.

Release notes

v0.1.0

  • Added static option to serve static files using json-server.
  • Starting to use semantic versioning.

v0.0.7

  • Fixed typo in server reloading sample and updated sample itself.

v0.0.6

  • Added reloading functionality. Now DB could be easily reloaded either from file or from object.
  • Added ability to kill the server.
  • Added deferredStart option, allowing to define server instance, but start it later.

v0.0.5

  • The id key, used to match objects in collections now could be changed using id parameter in options. Useful to simulate other DBs, for example MongoDB's _id.

v0.0.4

  • Added ability to change server's base URL.
  • Added ability to use rewrite rules.

v0.0.0 - v0.0.3

Basic version of plugin with ability to start json-server from specified file or object, on specific port.

License

MIT © Nikita Ivanov (http://borodatik.net)

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.