-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
138 lines (116 loc) · 3.43 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
require('babel-register');
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const fileUpload = require('express-fileupload');
// const methodOverride = require("method-override");
const expressSession = require('express-session')({
secret: 'random strings here',
resave: false,
saveUninitialized: false,
});
const webpack = require('webpack');
const flash = require('connect-flash');
const session = require("express-session")
const User = require('./models/user');
const PostAdd = require('./models/postAdd');
// Routes
const authentication = require('./routes/api/authentication');
const app = express();
app.use(fileUpload());
app.post("/upload", function(res,req){
if(!req.files){
res.send("No file uploaded");
}
else{
var file = req.files.file;
var extension = path.extname(file.name);
if(extension !=="png" && extension !== ".gif" && extension !==".jpg"){
res.send("only images are allowed");
}
else{
file.mv(__dirname+"/uploads/" + file.name, function(err){
if(err){
res.status(500).send(err);
}
else{
res.send("File Uploaded");
}
});
}
}
})
//View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
// app.use(methodOverride('_method'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('express-session')({
secret: 'any random string can go here',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api/authentication', authentication);
// Configure Passport
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.post('/insert', function(req, res, next){
var item =({
venueName : req.body.venueName,
email: req.body.email,
venueType: req.body.venueType,
occupancy: req.body.occupancy,
amenities: req.body.amenities,
date: req.body.date,
time: req.body.time,
price: req.body.price
}).save(function(err, res){
if(err){
res.json(err);
}
else{
res.send("inserted");
}
})
});
//catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
})
const PORT = process.env.PORT || 3001;
// If deployed, use the deployed database. Otherwise use the local mongoHeadlines database
var db = process.env.MONGODB_URI || "mongodb://localhost/rezUp";
// Connect mongoose to our database
mongoose.connect(db, function(error) {
// Log any errors connecting with mongoose
if (error) {
console.log(error);
}
// Or log a success message
else {
console.log("mongoose connection is successful");
}
});
app.listen(PORT, function(){
console.log(("Express server listening on port " + PORT));
});