-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.js
59 lines (46 loc) · 1.64 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var jsapi = require('./index'); // this is a regular Koa app
var data = {
posts: [
{id: 'p1', title: 'first post', body: 'lorem ip sum'},
{id: 'p2', title: 'second post', body: 'dolor sit amet'}
],
users: [
{id: 'u1', name: 'Chuck'}
],
home: {
title: 'My Title',
content: 'Lorem ipsum.'
}
};
// define custom API route for 'users'
data.users.route = 'local-users'
// define custom Koa middleware/routes if necessary
jsapi.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
jsapi.start(data, 3000);
// ########### Below is the testing part for the above server definition ############ //
console.log('Running example tests:')
var request = require('request');
var uri = 'http://localhost:3000/'
request({url: uri + 'local-users/u1', json: true}, function (error, response, body) {
console.log('get user #1:', body)
})
request({url: uri + 'posts/p1', json: true}, function (error, response, body) {
console.log('get post #1:', body)
})
request({url: uri + 'posts', method: 'POST', json: {id:'p3', title: 'third post', body: 'quick brown fox'}}, function (error, response, body) {
console.log('create post #3:', body)
})
request({url: uri + 'posts/p1', method: 'PUT', json: {id:'p1', title: 'first post', body: 'some proper body'}}, function (error, response, body) {
console.log('update post #1:', body)
})
request({url: uri + 'posts', json: true}, function (error, response, body) {
console.log('all posts:', body)
})
request({url: uri + 'home', json: true}, function (error, response, body) {
console.log('home page:', body)
})