-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
65 lines (57 loc) · 1.75 KB
/
index.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
//index.js
var express = require('express'),
MongoClient = require('mongodb').MongoClient,
app = express(),
mongoUrl = 'mongodb://localhost:27017/textmonkey';
var redisClient = require('redis').createClient;
var redis = redisClient(6379, "localhost");
var access = require('./access.js');
MongoClient.connect(mongoUrl, function(err, db) {
if (err)
throw 'Error connecting to database - ' + err;
app.post('/book', function(req,res) {
if (!req.body.title || !req.body.author)
res.status(400).send("Please send a title and an author for the book");
else if (!req.body.text)
res.status(400).send("Please send some text for the book");
else {
access.saveBook(db, req.body.title, req.body.author, req.body.text, function(err) {
if (err)
res.status(500).send("Server error");
else
res.status(201).send("Saved");
});
}
});
app.get('/book/:title', function(req,res) {
if (!req.param('title'))
res.status(400).send("Please send a proper title");
else {
access.findBookByTitleCached(db, redis, req.param('title'), function(book) {
if (!text)
res.status(500).send("Server error");
else
res.status(200).send(book);
});
}
});
app.put('/book/:title', function(req,res) {
if(!req.param("title"))
res.status(400).send("Please send the book title");
else if (!req.param("text"))
res.status(400).send("Please send the new text");
else {
access.updateBookByTitle(db, redis, req.param("title"), req.param("text"), function(err) {
if(err == "Missing book")
res.status(404).send("Book not found");
else if(err)
res.status(500).send("Server error");
else
res.status(200).send("Updated");
});
}
});
app.listen(8000, function() {
console.log('Listening on port 8000');
});
});