-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
133 lines (109 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
130
131
132
133
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose');
//config = require('/CONFIG/DB');
var morgan = require('morgan');
var passport = require('passport');
const multer = require('multer');
const fs = require('fs');
const http = require('http');
mongoose.Promise = require('bluebird');
const socketIO = require('socket.io');
var config ={
secret:'istanbul',
DB: 'mongodb://localhost:27017/istanbul'
}
mongoose.connect(config.DB, {
promiseLibrary: require('bluebird'),
useNewUrlParser: true
}).then(
() => {
console.log('Database is connected')
},
err => {
console.log('Can not connect to the database' + err)
}
);
var api = require('./routes/api');
const LocationsRoutes = require('./routes/Locations.routes');
const LocationsCategoriesRoutes = require('./routes/LocationsCategories.routes');
const AreasRoutes = require('./routes/areas.routes');
const RatingLocationsRoutes = require("./routes/RatingLocation.routes");
//const ImagesRoutes = require('./routes/images.routes');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
'extended': 'false'
}));
app.use(cors());
const port = process.env.PORT || 4000;
app.use(passport.initialize());
app.use('/api', api);
app.use('/Locations', LocationsRoutes);
app.use('/LocationsCategories', LocationsCategoriesRoutes);
app.use('/RatingLocation', RatingLocationsRoutes);
app.use('/areas', AreasRoutes);
// app.use('/images',ImagesRoutes);
//images start
const DIR = './uploads';
let storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, DIR);
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
let upload = multer({
storage: storage
});
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use('/uploads', express.static(process.cwd() + '/uploads'));
app.use(express.static(path.join(__dirname, './dist')));
app.get('*',(req,res) => res.sendFile(path.join(__dirname+'/dist/index.html')));
app.get('/api', function (req, res) {
res.end('file catcher example');
});
app.post('/api/upload', upload.single('photo'), function (req, res) {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log(req.file.filename);
return res.send({
filename: req.file.filename,
path: req.file.port
})
}
});
// images done
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
// process.exit();
next(err);
});
app.use(function (req, res, next) {
var err = new Error(res);
err.status = 404;
//res.status(404).end('error');
next(err);
});
const server = http.createServer(app);
const io = socketIO(server);
io.origins('*:*')
app.set('io', io);
server.listen(port);