This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (58 loc) · 1.77 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
// Connection with MongoDB
const database = require('./backend/config/mongoose');
// Authentication.
const passport = require('./backend/config/passport');
// Middleware.
const express = require('express');
// Express application.
const app = require('./backend/config/express')(passport);
// Setup routes.
require('./backend/routes/article')(app);
require('./backend/routes/article-category')(app);
const path = require('path');
const User = require('./backend/models/user');
/**
* Transforms the given user into a user profile,
* that can be sent to the browser.
*/
var get_profile = user => ({
id: user._id,
username: user.username,
screenname: user.screenname
});
// Routes.
// Login route.
app.post('/api/login',
// authenticate using passport.
passport.authenticate('local'),
function(req, res) {
if(req.user) {
req.session.user = req.user;
var user = get_profile(req.user);
console.log(`Braunian Noise: ${user.username} logged in.`);
res.json(user);
}
else {
res.status(401).send('Invalid username or password.');
}
}
);
// Logout route.
app.post('/api/logout', function(req, res) {
console.log(`Braunian Noise: User logged out.`);
req.logout();
res.status(200).send({text: 'Successfully logged out!'});
});
app.get('/api/test', function(req, res){
console.log(req);
res.status(200).send({text: 'Test!'});
})
// Main entry point for serving the Angular app.
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.static(path.join(__dirname, 'uploads')));
app.get('/*', (req, res) => {
res.sendFile('index.html', {root: path.join(__dirname, 'dist')});
});
// Start server.
app.listen(8080);
console.log("Braunian Noise: Ready to Rumble! - Listening on port 8080.");