-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (118 loc) · 3.4 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var express = require('express'),
mongodb = require("mongodb"),
bodyParser = require('body-parser'),
anagrams = require('./routes/anagrams'),
words = require('./routes/words');
var app = express();
var ObjectID = mongodb.ObjectID;
app.use(bodyParser.json({limit: '50mb'})); // support (large) json encoded bodies
// Create a database variable outside of the database connection callback to reuse the connection pool
global.db = null;
// Setup the mongodb callback
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = database;
console.log("Mongo connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 3000, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// Route configuration below
/**
* @api {POST} /words
* @apiName AddWords
* @apiGroup words
*
* @apiDescription Takes a JSON array of English-language words and adds them to the corpus (data store).
*
* @apiSampleRequest /words.json
*
* @apiExample cURL example
* $ curl -i -X POST -d '{ "words": ["read", "dear", "dare"] }' http://localhost:3000/words.json
*
* @apiSuccessExample {js} Success-Response:
* HTTP/1.1 201 CREATED
*/
app.post('/words.json', words.add);
/**
* @api {DELETE} /words/:word
* @apiName DeleteWord
* @apiGroup words
*
* @apiParam word the word being deleted
*
* @apiDescription Removes a single word from the corpus
*
* @apiSampleRequest /words/read.json
*
* @apiExample cURL example
* $ curl -i -X DELETE http://localhost:3000/words/read.json
*
* @apiSuccessExample {js} Success-Response:
* HTTP/1.1 200 OK
*/
app.delete('/words/:word.json', words.deleteWord);
/**
* @api {DELETE} /words
* @apiName DeleteCorpus
* @apiGroup words
*
* @apiParam word the word being deleted
*
* @apiDescription Removes a single word from the corpus
*
* @apiSampleRequest /words.json
*
* @apiExample cURL example
* $ curl -i -X DELETE http://localhost:3000/words.json
*
* @apiSuccessExample {js} Success-Response:
* HTTP/1.1 204 No Content
*/
app.delete('/words.json', words.deleteCorpus);
/**
* @api {GET} /anagrams/:word
* @apiName GetAnagrams
* @apiGroup anagrams
*
* @apiParam word the word for which anagrams will be generated
* @apiParam limit Optional. the maximum number of results to return
*
* @apiDescription Returns a JSON array of English-language words that are anagrams of the word passed in the URL.
*
* @apiSampleRequest /anagrams/read.json?max_results
*
* @apiExample cURL example
* $ curl -i http://localhost:3000/anagrams/read.json
*
* @apiSuccessExample {js} Success-Response:
* HTTP/1.1 200 OK
* {
* "anagrams": [ "dear", "dare" ]
* }
*/
app.get('/anagrams/:word.json', anagrams.get);
/**
* @api {DELETE} /anagrams/:word
* @apiName DeleteAnagrams
* @apiGroup anagrams
*
* @apiParam word the word for which anagrams will be generated
*
* @apiDescription Removes a word from the corpus along with its anagrams
*
* @apiSampleRequest /anagrams/read.json
*
* @apiExample cURL example
* $ curl -i -X DELETE http://localhost:3000/anagrams/read.json
*
* @apiSuccessExample {js} Success-Response:
* HTTP/1.1 204 No Content
*/
app.delete('/anagrams/:word.json', anagrams.delete);