-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (96 loc) · 3.03 KB
/
Copy pathserver.js
File metadata and controls
115 lines (96 loc) · 3.03 KB
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
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 cors = require("cors");
const passport = require("./config/passport")();
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(cors());
app.use(express.json());
app.use(passport.initialize());
// app.get('/test', (req, res)=>{
// res.status(200).json({
// website: 'Spoofy',
// info: 'Music playlist app'
// })
// })
//CONTROLLER START
const userController = require("./controllers/users.js");
app.use("/users", userController);
app.use('/api/music', require('./controllers/music.js'))
//CONTROLLER END
//LISTENER
/*Catch routes nested two levels deep */
// app.use('/:id/', express.static('public'))
/* Catch routes nested two levels deep */
// app.get('*', (req, res)=>{
// res.sendFile(path.resolve(`${__dirname}/public/index.html`));
// })
//LISTENER
app.listen(PORT, () => {
console.log(`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
*/