-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (112 loc) · 3.28 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
130
131
132
133
134
135
136
137
138
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const multer = require('multer');
const Grid = require('gridfs-stream');
const {GridFsStorage} = require('multer-gridfs-storage');
const path = require('path');
const Pusher = require('pusher');
//const connectDB = require('./config/db');
const facePosts = require('./models/facePosts');
// routes
Grid.mongo = mongoose.mongo;
const app = express();
const pusher = new Pusher({
appId: "1383515",
key: "6bcabaee92f3e67baee2",
secret: "fae1664d9b93bbb344bd",
cluster: "sa1",
useTLS: true
});
// Connect Database
const mongoURI = ""
const conn = mongoose.createConnection(mongoURI);
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB');
const changeStream = mongoose.connection.collection('posts').watch();
changeStream.on('change', (change) => {
console.log(change);
if (change.operationType === 'insert') {
pusher.trigger('posts', 'inserted', {
change: change
})
} else {
console.log('Error in change stream');
}
});
})
let gfs;
conn.once("open", () => {
console.log("Connected to MongoDB");
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection("images");
});
// Create storage engine
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename=`image-${Date.now()}${path.extname(file.originalname)}`
const fileInfo = {
filename: filename,
bucketName: "images"
};
resolve(fileInfo);
});
}
});
const upload = multer({ storage });
mongoose.connect(mongoURI);
// cors
app.use(cors({ origin: true, credentials: true }));
// Init Middleware
app.use(express.json());
app.use(cors());
app.use(bodyParser.json());
app.set('view engine', 'ejs');
// use Routes
app.get("/", (req, res) => res.status(200).send("<h1>Hi Programmers!!!</h1>"));
app.post("/upload/post", (req, res) => {
const dbPost = req.body;
console.log(dbPost);
facePosts.create(dbPost, (err, data) => {
if (err) {
res.status(500).send(err)
} else {
res.status(201).send(data)
}
})
})
app.get("/retrieve/posts", (req, res) => {
facePosts.find((err, data) => {
if (err) {
res.status(500).send(err);
} else {
data.sort((b, a) => {
return a.timestamp - b.timestamp;
});
res.status(200).send(data);
}
});
});
app.post("/upload/image", upload.single("file"), (req, res) => {
res.status(201).send(req.file);
});
app.get("/retrieve/image/single", (req, res) => {
gfs.files.findOne({filename: req.query.name}, (err, file) => {
if(err) {
res.status(500).send(err);
}else{
if(!file || file.length === 0) { // if(!file)
res.status(404).json({err: 'No file found'});
}else{
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
}
}
});
});
//listen
const port = process.env.PORT || 8082;
app.listen(port, () => console.log(`Server running on port ${port}`));