Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active May 16, 2017 07:59
Show Gist options
  • Save xgqfrms-GitHub/cb8459af5e7db7af107a25615f738ab5 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/cb8459af5e7db7af107a25615f738ab5 to your computer and use it in GitHub Desktop.
json-server
@xgqfrms-GitHub
Copy link
Author

db.josn

{
    "posts": [
        {
            "id": 1,
            "title": "json-server1",
            "author": "typicode1"
        },
        {
            "id": 2,
            "title": "json-server2",
            "author": "typicode2"
        },
        {
            "id": 3,
            "title": "json-server3",
            "author": "typicode3"
        }
    ],
    "comments": [
        {
            "id": 1,
            "body": "some comment1",
            "postId": 1
        },
        {
            "id": 2,
            "body": "some comment2",
            "postId": 2
        },
        {
            "id": 3,
            "body": "some comment3",
            "postId": 3
        }
    ],
    "profile": {
        "name": "typicode1",
        "name": "typicode2",
        "name": "typicode3"
    }
}

@xgqfrms-GitHub
Copy link
Author


http://localhost:3000/profile


```json

{
    "name": "typicode"
}

http://localhost:3000/comments

[
    {
        "id": 1,
        "body": "some comment",
        "postId": 1
    }
]

http://localhost:3000/posts

[
    {
        "id": 1,
        "title": "json-server",
        "author": "typicode"
    }
]

http://localhost:3000/db

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Mar 26, 2017

@xgqfrms-GitHub
Copy link
Author

faker

https://www.npmjs.com/package/faker

$ npm install faker -D

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Mar 26, 2017

var/let xyz = null, undefined, "string", 0(NaN), false, [], {}, Symbol ?

变量初始化 赋值???

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol

/*

    var firstName = null;
    // undefined
    firstName;
    // null
    typeof(firstName);
    // "object"


    var lastName;
    // undefined
    lastName;
    // undefined
    typeof(lastName);
    // "undefined"


    var firstName = "";
    // undefined
    firstName;
    // ""
    typeof(firstName);
    // "string"


    var lastName = undefined;
    // undefined
    lastName;
    // undefined
    typeof(lastName);
    // "undefined"

    var firstName = 0;
    // undefined
    firstName;
    // 0
    typeof(firstName);
    // "number"


    var firstName = [];
    // undefined
    firstName;
    // []
    typeof(firstName);
    // "object"

    var firstName = {};
    // undefined
    firstName;
    // Object {}
    typeof(firstName);
    // "object"


    var firstName = NaN;
    // undefined
    firstName;
    // NaN
    typeof(firstName);
    // "number"


    var firstName = false;
    // undefined
    firstName;
    // false
    typeof(firstName);
    // "boolean"

    var firstName = Symbol();
    // undefined
    firstName;
    // Symbol()
    typeof(firstName);
    // "symbol"

    var firstName = Symbol("this is a symbol");
    // undefined
    firstName;
    // Symbol(this is a symbol)
    typeof(firstName);
    // "symbol"

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol

*/

@xgqfrms-GitHub
Copy link
Author

json-server & faker.js

$ json-server --watch db.json
$ json-server --watch fakerdb.js

demo

var faker = require('faker');

function generateEmployees(){
    var employees = [];
    var num = 30;
    var firstName = null,
        lastName = null,
        email = null;
    for( var i = 0; i < num; i++){
        firstName = faker.name.firstName();
        lastName = faker.name.lastName();
        email = faker.internet.email();

        employees.push({
            "id" : i,
            "firstName" : firstName,
            "lastName" : lastName,
            " email" : email
        });
    }
    return {
        "employees" : employees
    }
}

module.exports = generateEmployees;

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Mar 26, 2017

Table of contents

## Table of contents

<details>

<!-- toc -->

- [Example](#example)
- [Install](#install)
- [Routes](#routes)
  * [Plural routes](#plural-routes)
  * [Singular routes](#singular-routes)
  * [Filter](#filter)
  * [Paginate](#paginate)
  * [Sort](#sort)
  * [Slice](#slice)
  * [Operators](#operators)
  * [Full-text search](#full-text-search)
  * [Relationships](#relationships)
  * [Database](#database)
  * [Homepage](#homepage)
- [Extras](#extras)
  * [Static file server](#static-file-server)
  * [Alternative port](#alternative-port)
  * [Access from anywhere](#access-from-anywhere)
  * [Remote schema](#remote-schema)
  * [Generate random data](#generate-random-data)
  * [HTTPS](#https)
  * [Add custom routes](#add-custom-routes)
  * [Add middlewares](#add-middlewares)
  * [CLI usage](#cli-usage)
  * [Module](#module)
    + [Simple example](#simple-example)
    + [Custom routes example](#custom-routes-example)
    + [Access control example](#access-control-example)
    + [Custom output example](#custom-output-example)
    + [Rewriter example](#rewriter-example)
    + [Mounting JSON Server on another endpoint example](#mounting-json-server-on-another-endpoint-example)
  * [Deployment](#deployment)
- [Links](#links)
  * [Video](#video)
  * [Articles](#articles)
  * [Third-party tools](#third-party-tools)
- [License](#license)

<!-- tocstop -->

</details>

@xgqfrms-GitHub
Copy link
Author

var faker = require('faker');

function generateEmployees(){
    var employees = {};
    var temp =[];
    var num = 10;
    var firstName = null,
        lastName = null,
        email = null;
    for( var i = 0; i < num; i++){
        firstName = faker.name.firstName();
        lastName = faker.name.lastName();
        email = faker.internet.email();

        // Object.assign({}, {
        //     "id" : i,
        //     "firstName" : firstName,
        //     "lastName" : lastName,
        //     "email" : email
        // });
        temp.push({
            "id" : i+1,
            "firstName" : firstName,
            "lastName" : lastName,
            "email" : email
        });
    }
    Object.assign(employees,temp);
    return {
        "employees" : employees
    }
}

module.exports = generateEmployees;

/*

$ json-server --watch db.json

$ json-server --watch faker-object.js

http://localhost:3000/

http://localhost:3000/db

http://localhost:3000/employees

*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment