-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (84 loc) · 2.89 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
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
const mongoose = require("mongoose");
const express = require("express");
const bodyParser = require("body-parser");
const passport = require("passport");
const db = require("./config/keys").mongoURI;
const users = require("./routes/api/users");
const playlists = require("./routes/api/playlists");
const songs = require('./routes/api/songs');
const follows = require('./routes/api/follows');
const answers = require('./routes/api/answers');
const posts = require('./routes/api/posts');
const seedDb = require('./seeds');
require("./config/passport")(passport);
const app = express();
const path = require('path');
if (process.env.NODE_ENV === 'production') {
app.use(express.static('frontend/build'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
})
}
const questions = require("./routes/api/questions");
let gfs;
mongoose
.connect(db, { useNewUrlParser: true })
.then(() =>{
// seedDb();
console.log("Connected to MongoDB successfully")
})
.catch((err) => console.log(err));
const conn = mongoose.connection;
conn.once("open", function () {
gfs = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: 'photos'
});
});
// allows for postman tests
app.use(bodyParser.urlencoded({ extended: false }));
// tells app to only respond to json
app.use(bodyParser.json());
// to display single file object
app.get("/api/file/:filename", (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
return res.json(file);
})
});
// to render the image
app.get("/api/image/:filename", (req, res) => {
gfs.find({ filename: req.params.filename }).toArray((err, files) => {
if (!files[0] || files.length === 0) {
return res.status(200).json({
success: false,
message: 'No files available',
});
}
if (files[0].contentType === 'image/jpeg' || files[0].contentType === 'image/png' || files[0].contentType === 'image/svg+xml') {
// render image to browser
gfs.openDownloadStreamByName(req.params.filename).pipe(res);
} else {
res.status(404).json({
err: 'Not an image',
});
}
});
});
app.use(passport.initialize());
// Routes
app.use("/api/spotify/songs", require("./routes/api/spotify/songs"));
app.use("/api/spotify/login", require("./routes/api/spotify/login"))
app.use("/api/spotify/playlists", require("./routes/api/spotify/playlists"))
app.use("/api/users", users);
app.use('/api/songs', songs);
app.use('/api/answers', answers);
app.use('/api/playlists', playlists);
app.use('/api/questions', questions);
app.use('/api/follows', follows);
app.use('/api/posts', posts);
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server is running on port ${port}`));