-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (81 loc) · 2 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
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require('express');
// Start up an instance of app
const app = express();
/* Dependencies */
const bodyParser = require('body-parser');
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cors for cross origin allowance
const cors = require('cors');
app.use(cors());
// Initialize the main project folder
app.use(express.static('website'));
// Setup Server
const port = 8000;
const server = app.listen(port, listening);
function listening() {
console.log('server running');
console.log(`running on localhost: ${port}`);
}
// Adding emoji to content
const emoji = require('node-emoji');
function emojiName(feelings) {
switch(feelings) {
case 'happy':
case 'good':
case 'great':
return 'smiley';
break;
case 'sad':
case 'bad':
case 'depressed':
return 'disappointed';
break;
case 'afraid':
case 'scared':
return 'fearful';
break;
case 'sick':
return 'mask';
break;
case 'surprised':
return 'open_mouth';
break;
case 'crying':
return 'cry';
break;
case 'hungry':
return 'yum';
break;
case 'in love':
return 'heart_eyes';
break;
default:
return feelings;
}
}
// GET route
app.get('/all', sendData);
function sendData (req, res) {
res.send(projectData);
}
// POST route
app.post('/add', addData);
function addData (req, res) {
projectData.date = req.body.date;
projectData.temp = req.body.temp;
projectData.icon = req.body.icon;
projectData.content = req.body.content;
if (emoji.hasEmoji(emojiName(req.body.content.toLowerCase()))) {
projectData.emoji = emoji.get(emojiName(req.body.content.toLowerCase()));
} else {
projectData.emoji = '';
}
res.send(projectData);
console.log(projectData);
}