-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
116 lines (104 loc) · 2.81 KB
/
server.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Very simple NodeJS API to illustrate the usage of angular-scaffold
*
* Creates an API around adding, listing and removing posts, which are angular-model
* instances that simply have a name and an identifier.
*
* This API provides:
* * A basic web server serving static assets, based from index.html
* * A single API endpoint, /post, with GET, POST and DELETE implemented (PUT
* is left as an exercise to the reader)
*
* This serves to demonstrate that angular-scaffold can talk to a REST-ish API.
*
* Install dependencies with:
*
* npm install
*
* Start it with:
*
* node server.js
*
* Then browse to http://localhost:4730/
*/
// acquire dependencies
var express = require('express');
var bodyParser = require('body-parser');
// Configure the Express application
var app = express();
// Use the body parser json plugin so that new posts can be created
app.use(bodyParser.json());
// serve this directory statically so users can just browse it as well as use the API
app.use(express.static('.'));
// Declare a set of posts. This is the data that the API will start with.
// Posts can be added and removed through example page index.html, although
// once you restart the server, it
var posts = [{
"$links": {
"self": "/api/posts/posta"
},
"_id":"posta",
"name":"Post A",
"body":"Body for post A, it's just some text you know"
}, {
"$links": {
"self": "/api/posts/postb"
},
"_id":"postb",
"name":"Post B",
"body":"Body for post B"
}];
/**
* Go through the posts collection and find a post where post._id = id
*
* This is a very simple, naive implementation, it is not production code, it is
* supplied solely as part of this sample API.
*
* @param {int} id
* @returns angular-model instance or null
*/
function findPostIndex(id) {
for (var i = 0 ; i < posts.length; i++) {
if (posts[i]._id === id) {
return i;
}
}
return null;
}
// Set up the /posts endpoint
/**
* GET /posts will return the posts in JSON format
*/
app.get('/api/posts', function(req, res) {
res.json(posts);
});
/**
* POST /posts will create a new post
*
* There is no error checking here. This is a naive example API only.
*/
app.post('/api/posts', function(req, res) {
var newPost = {
$links: {
self: "/posts/" + req.body._id
},
_id: req.body._id,
name: req.body.name,
body: req.body.name
};
posts.push(newPost);
res.json(true);
});
/**
* DELETE /posts/id will delete the post identified by request parameter id
*
* There is no error checking here. This is a naive example API only.
*/
app.delete('/api/posts/:id', function(req, res) {
var idx = findPostIndex(req.params.id);
posts.splice(idx, 1);
res.json(true);
});
// Start this example API on localhost:4730
app.listen(process.env.PORT || 4730);
console.log("Running on http://localhost:4730/");