-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiServer.js
140 lines (124 loc) · 3.45 KB
/
apiServer.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
130
131
132
133
134
135
136
137
138
139
140
"use strict"
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// APIs
var mongoose = require('mongoose');
/* mongoose.connect('mongodb://localhost:27017/bookshop', { useMongoClient: true })
const db = mongoose.connection; */
mongoose.connect('mongodb://testUser1:[email protected]:21413/bookshop', { useMongoClient: true })
const db = mongoose.connection;
db.on('error', console.error.bind(console, '# MongoDB - connection error: '));
// --->>> SET UP SESSIONS <<<----
app.use(session({
secret: 'mySecretString',
saveUninitialized: false,
resave:false,
cookie: {maxAge: 1000 * 60 * 60 * 24 * 2}, // 2 days in milliseconds
store: new MongoStore({mongooseConnection: db, ttl: 2 * 24 * 60 * 60})
//ttl: 2 days * 24 hours * 60 minutes * 60 seconds
}))
// SAVE SESSION CART API
app.post('/cart', function(req, res){
var cart = req.body;
req.session.cart = cart;
req.session.save(function(err){
if(err){
throw err;
}
res.json(req.session.cart);
})
});
// GET SESSION CART API
app.get('/cart', function(req, res){
if(typeof req.session.cart !== 'undefined'){
res.json(req.session.cart);
}
});
//--->>> END SESSION SET UP <<<----
var Books = require('./models/books.js');
//---->>> POST BOOKS <<<-----
app.post('/books', function(req, res){
var book = req.body;
Books.create(book, function(err, books){
if(err){
throw err;
}
res.json(books);
})
});
//----->>>> GET BOOKS <<<---------
app.get('/books', function(req, res){
Books.find(function(err, books){
if(err){
throw err;
}
res.json(books)
})
});
//---->>> DELETE BOOKS <<<------
app.delete('/books/:_id', function(req, res){
var query = {_id: req.params._id};
Books.remove(query, function(err, books){
if(err){
console.log("# API DELETE BOOKS: ", err);
}
res.json(books);
})
});
//---->>> UPDATE BOOKS <<<------
app.put('/books/:_id', function(req, res){
var book = req.body;
var query = req.params._id;
// if the field doesn't exist $set will set a new field
var update = {
'$set':{
title:book.title,
description:book.description,
image:book.image,
price:book.price
}
};
// When true returns the updated document
var options = {new: true};
Books.findOneAndUpdate(query, update, options, function(err, books){
if(err){
throw err;
}
res.json(books);
})
})
app.get('/images', function(req, res){
const imgFolder = __dirname + '/public/images/';
// REQUIRE FILE SYSTEM
const fs = require('fs');
//READ ALL FILES IN THE DIRECTORY
fs.readdir(imgFolder, function(err, files){
if(err){
return console.error(err);
}
//CREATE AN EMPTY ARRAY
const filesArr = [];
//var i = 1;
// ITERATE ALL IMAGES IN THE DIRECTORY AND ADD TO THE ARRAY
files.forEach(function(file){
filesArr.push({name: file});
//i++
});
// SEND THE JSON RESPONSE WITH THE ARARY
res.json(filesArr);
})
})
// END APIs
app.listen(3001, function(err){
if(err){
return console.log(err);
}
console.log('API Sever is listening on http://localhost:3001');
});