-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (97 loc) · 3.46 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
require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8000;
const mongoose = require('mongoose');
const path = require('path');
const announcementController = require ('./controllers/announcementControllers');
const flavorController = require ('./controllers/flavorControllers');
const contactSubmissonController = require ('./controllers/contactSubmissionControllers');
const orderController = require ('./controllers/orderControllers');
const disabledDateController = require ('./controllers/disabledDateControllers');
const { hash, register, login } = require('./controllers/auth');
const MONGODB_URI = process.env.MONGODB_URI
const db = mongoose.connection;
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
db.on('open', () => {
console.log('Mongo is Connected');
});
/* Middleware */
app.use(express.json());
if (process.env.NODE_ENV !== 'development'){
app.use(express.static('public'))
}
/* Controller Goes Here Remove the tes*/
app.use('/api/announcement', announcementController);
app.use('/api/orders',orderController);
app.use('/api/contactsubmissions', contactSubmissonController);
app.post('/api/register', register);
app.post('/api/login', login);
app.use('/api/register', require('./controllers/userControllers'));
app.use('/api/flavor', flavorController);
app.use('/api/disableddate',disabledDateController);
/* Controller Ends here */
//LISTENER
// for react router
app.get('*', (req, res) => {
res.sendFile(path.resolve(path.join(__dirname, 'public', 'index.html')))
})
app.listen(PORT, () => {
console.log(`API Listening on port ${PORT}`);
});
/* Vanilla Node Server
const http = require('http'); // The node http module allow you to create servers
const fs = require('fs'); // The node file system module allows you to create files and interact with file system
const path = require('path'); // path allows you to get the path of a folder etc.
const PORT = process.env.PORT || 8080;
const public = __dirname + '/public'
http.createServer(function (req, res) {
let filePath = public + req.url;
if (filePath == public + '/') {
filePath = public + '/index.html';
}
filePath = filePath.split('?')[0]
let extName = String(path.extname(filePath)).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
let contentType = mimeTypes[extName] || 'application/octet-stream';
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT') {
fs.readFile(public + '/404.html', function(error, content) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(content, 'utf-8');
});
}
else {
res.writeHead(500);
res.end('Sorry, you got an error bro here it is'+error.code+' ..\n');
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}).listen(PORT);
console.log(`Server started! Listening on port: ${PORT}`);
// basic node server without express serving
*/