generated from Elevate-Lab/Import-Template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
129 lines (102 loc) · 3.3 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
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const User = require("./models/userModel");
const cors = require('cors')
const bodyParser = require('body-parser')
// imp packages for file uploads
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
require("dotenv/config");
const port = process.env.PORT || 5000;
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(
cors({
origin: "http://localhost:3000", // All requests from this server will be intercepted here.
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true // Facilitates browser session cookies
})
);
/* -------- Set up session ------------*/
app.use(session({
secret: "Our little secret.",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
/* -------- Session set up ended ------------*/
/* -------- Passport serialise ------------*/
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
/* -------- Passport serialise ended ------------*/
/* -------- Passport serialise ended ------------*/
/*Routes Config*/
const indexRoute = require("./routes/index");
const registerRoute = require('./routes/auth/register');
const loginRoute = require('./routes/auth/login');
const googleAuth = require('./routes/auth/googleAuth');
const userOperation = require('./routes/user/user_operations');
const postsRoute = require('./routes/post/postRoute');
const commentRoute = require('./routes/comment/commentRoute');
/*-----Routes Config End------*/
/*App Config*/
app.use("/", indexRoute);
app.use("/register", registerRoute);
app.use("/login", loginRoute);
app.use("/auth/google", googleAuth);
app.use("/user", userOperation);
app.use("/posts", postsRoute);
app.use("/comments", commentRoute);
/*------App Config End--------*/
/* Mongoose config*/
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017/sereton-inn";
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
const connection = mongoose.connection;
let gfs;
connection.once("open", () => {
console.log("MongoDB database connection established successfully");
//Initialize our stream
gfs = Grid(connection.db, mongoose.mongo);
gfs.collection('imageUpload');
});
app.get('/image/:imageName', (req, res) => {
gfs.files.findOne({ filename: req.params.imageName }, (err, file) => {
//check if files exist
if (!file || file.length == 0) {
return res.status(404).json({
err: "No files exist"
})
}
//check if image
if (file.contentType === 'image/jpeg' || file.contentType === "image/png") {
//read output to browser
const readStream = gfs.createReadStream(file.filename)
readStream.pipe(res)
} else {
res.status(404).json({
err: "Not an image"
})
}
})
});
/*----Mongoose config End-----*/
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});