├── .gitignore ├── package.json ├── README.md └── faker.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | npm-debug.log 4 | .tmp 5 | dist 6 | .sass-cache 7 | app/assets/vendor 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "api", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "start": "./node_modules/.bin/json-server faker.js" 7 | }, 8 | "devDependencies": { 9 | "faker": "^3.0.1", 10 | "json-server": "^0.8.2" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blog Fake API 2 | 3 | Use it to play with your Front-end MV* JS. 4 | 5 | ### Install: 6 | ``` 7 | npm install 8 | ``` 9 | 10 | ### Run: 11 | ``` 12 | npm start 13 | ``` 14 | 15 | ## Routes 16 | 17 | ### Resources 18 | 19 | - http://localhost:3000/users 20 | - http://localhost:3000/categories 21 | - http://localhost:3000/posts 22 | - http://localhost:3000/comments 23 | - http://localhost:3000/profile 24 | 25 | ### Home 26 | http://localhost:3000 27 | 28 | ### Plural routes 29 | 30 | ``` 31 | GET /posts 32 | GET /posts/1 33 | POST /posts 34 | PUT /posts/1 35 | PATCH /posts/1 36 | DELETE /posts/1 37 | ``` 38 | 39 | ### Singular routes 40 | 41 | ``` 42 | GET /profile 43 | POST /profile 44 | PUT /profile 45 | PATCH /profile 46 | ``` 47 | 48 | ### Filter 49 | 50 | Use `.` to access deep properties 51 | 52 | ``` 53 | GET /posts?title=Foo 54 | GET /posts?id=1&id=2 55 | GET /comments?user.name=John 56 | ``` 57 | 58 | ### Slice 59 | 60 | Add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response) 61 | 62 | ``` 63 | GET /posts?_start=20&_end=30 64 | GET /posts/1/comments?_start=20&_end=30 65 | GET /posts?_limit=10 66 | ``` 67 | 68 | ### Sort 69 | 70 | Add `_sort` and `_order` (ascending order by default) 71 | 72 | ``` 73 | GET /posts?_sort=views&_order=DESC 74 | ``` 75 | 76 | ### Range 77 | 78 | Add `_gte` or `_lte` 79 | 80 | ``` 81 | GET /posts?views_gte=10&views_lte=20 82 | ``` 83 | 84 | ### Full-text search 85 | 86 | Add `q` 87 | 88 | ``` 89 | GET /posts?q=internet 90 | ``` 91 | 92 | ### Relationships 93 | 94 | To include children resources, add `_embed` 95 | 96 | ``` 97 | GET /posts?_embed=comments 98 | GET /posts/1?_embed=comments 99 | ``` 100 | 101 | To include parent resource, add `_expand` 102 | 103 | ``` 104 | GET /comments?_expand=post 105 | GET /comments/1?_expand=post 106 | ``` 107 | 108 | To get nested resources (by default one level, [add routes](#add-routes) for more) 109 | 110 | ``` 111 | GET /posts/1/comments 112 | ``` 113 | -------------------------------------------------------------------------------- /faker.js: -------------------------------------------------------------------------------- 1 | var faker = require('faker'); 2 | 3 | var seedUsers = function(quantity, data) { 4 | for (var i = 1; i <= quantity; i++) { 5 | var gender = faker.random.number({ min: 1, max: 2 }), 6 | firstName = faker.name.firstName(gender), 7 | lastName = faker.name.lastName(gender); 8 | 9 | data.users.push({ 10 | id: i, 11 | name: faker.name.findName(firstName, lastName, gender), 12 | username: faker.internet.userName(firstName, lastName), 13 | email: faker.internet.email(firstName, lastName).toLowerCase(), 14 | gender: gender, 15 | dateOfBirth: faker.date.past(55, new Date()), 16 | ocuppation: faker.name.jobTitle(), 17 | avatar: faker.internet.avatar() 18 | }); 19 | } 20 | }; 21 | 22 | var seedCategories = function(quantity, data) { 23 | String.prototype.capitalize = function() { 24 | return this.charAt(0).toUpperCase() + this.slice(1); 25 | } 26 | 27 | for (var i = 1; i <= quantity; i++) { 28 | data.categories.push({ 29 | id: i, 30 | name: faker.lorem.words( 31 | faker.random.number({ min: 1, max: 2 }) 32 | ).join(' ').capitalize() 33 | }); 34 | } 35 | }; 36 | 37 | var seedPosts = function(quantity, data) { 38 | for (var i = 1; i <= quantity; i++) { 39 | var title = faker.lorem.sentence(); 40 | 41 | data.posts.push({ 42 | id: i, 43 | title: title, 44 | body: faker.lorem.paragraphs(faker.random.number({ min: 2, max: 12 })), 45 | image: faker.image.image(), 46 | views: faker.random.number(1500), 47 | recommends: faker.random.number(50), 48 | userId: faker.random.number({ min: 1, max: data.users.length }), 49 | categoryId: faker.random.number({ min: 1, max: data.categories.length }) 50 | }); 51 | } 52 | }; 53 | 54 | var seedComments = function(quantity, data) { 55 | for (var i = 1; i <= quantity; i++) { 56 | data.comments.push({ 57 | id: i, 58 | body: faker.lorem.sentences(faker.random.number({ min: 1, max: 5 })), 59 | userId: faker.random.number({ min: 1, max: data.users.length }), 60 | postId: faker.random.number({ min: 1, max: data.posts.length }) 61 | }); 62 | } 63 | }; 64 | 65 | var seedProfile = function(data) { 66 | data.profile = faker.random.arrayElement(data.users); 67 | }; 68 | 69 | var db = function() { 70 | var data = { 71 | users: [], 72 | categories: [], 73 | posts: [], 74 | comments: [], 75 | profile: {} 76 | }; 77 | 78 | seedUsers(100, data); 79 | seedCategories(30, data); 80 | seedPosts(150, data); 81 | seedComments(300, data); 82 | seedProfile(data); 83 | 84 | return data; 85 | }; 86 | 87 | module.exports = db; 88 | --------------------------------------------------------------------------------