-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathapp.js
57 lines (46 loc) · 1.5 KB
/
app.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
const express = require('express');
const exphbs = require('express-handlebars');
const MongoClient = require('mongodb').MongoClient
const uri = "mongodb://admin:12345678@db:27017";
MongoClient.connect(uri)
.then(main)
.catch(console.error)
function main(client) {
const db = client.db('booksdb');
const app = express();
// Configure Handlebars, the view engine
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs'
}));
app.set('view engine', 'hbs');
app.use(express.static('views/images'));
app.get('/', function (req, res) {
const category = req.query.category;
const query = (category !== undefined && category.length > 0) ? { categories: category } : {};
db.collection('Books')
.find(query)
.toArray()
.then(books => {
res.render('home', {
books
});
});
});
app.get('/search', function (req, res) {
// db.users.findOne({"username" : {$regex : ".*son.*"}});
const key = req.query.q;
const query = (key !== undefined && key.length > 0) ? { title: { $regex: `.*${key}.*`} } : {};
db.collection('Books')
.find(query)
.toArray()
.then(books => {
res.render('home', {
books
});
});
});
app.listen(3000, () => {
console.log('The web server has started on port 3000');
});
}