forked from phazonoverload/pusher-photowall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (64 loc) · 1.88 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
require('dotenv').config({
path: 'variables.env'
});
const express = require('express');
const multipart = require('connect-multiparty');
const bodyParser = require('body-parser');
const cloudinary = require('cloudinary');
const cors = require('cors');
const Datastore = require('nedb');
const Pusher = require('pusher');
const path = require("path");
const app = express();
const db = new Datastore();
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_APP_KEY,
secret: process.env.PUSHER_APP_SECRET,
cluster: process.env.PUSHER_APP_CLUSTER,
encrypted: true,
});
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
app.use(cors());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client')));
const multipartMiddleware = multipart();
app.get('/', (req, res) => {
db.find({}, (err, data) => {
if (err) return res.status(500).send(err);
res.json(data);
});
});
app.post('/upload', multipartMiddleware, (req, res) => {
cloudinary.v2.uploader.upload(req.files.image.path, {}, (error, result) => {
if (error) {
return res.status(500).send(error);
}
db.insert(Object.assign({}, result, req.body), (err, newDoc) => {
if (err) {
return res.status(500).send(err);
}
pusher.trigger('gallery', 'upload', {
image: newDoc,
});
res.status(200).json(newDoc);
});
});
});
app.get('/upload', function(req, res) {
res.sendfile('./client/upload.html');
});
app.get('/wall', function(req, res) {
res.sendfile('./client/index.html');
});
app.set('port', process.env.PORT || 5000);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});