-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·120 lines (104 loc) · 3.41 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
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var shortid = require('shortid')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
var port = process.env.PORT || 8080
var router = express.Router()
// Unsafely enable cors
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
// logging middleware
router.use(function(req, res, next) {
console.log('\nReceived:',{url: req.originalUrl, body: req.body, query: req.query})
next()
})
// Simple in memory database
const database = [
{ name: 'Tea Chats', id: 0, users: ['Ryan','Nick', 'Danielle'], messages: [{name: 'Ryan', message: 'ayyyyy', id: 'gg35545', reaction: null},{name: 'Nick', message: 'lmao', id: 'yy35578', reaction: null}, {name: 'Danielle', message: 'leggooooo', id: 'hh9843', reaction: null}]},
{ name: 'Coffee Chats', id: 1, users: ['Jessye'], messages: [{name: 'Jessye', message: 'ayy', id: 'ff35278', reaction: null}]}
]
// Utility functions
const findRoom = (roomId) => {
const room = database.find((room) => {
return room.id === parseInt(roomId)
})
if (room === undefined){
return {error: `a room with id ${roomId} does not exist`}
}
return room
}
const findRoomIndex = (roomId) => {
const roomIndex = database.findIndex((room) => {
return room.id === parseInt(roomId)
})
return roomIndex
}
const findMessageIndex = (room, messageId) => {
const messageIndex = room.messages.findIndex((message) => {
return message.id === messageId
})
return messageIndex
}
const logUser = (room, username) => {
const userNotLogged = !room.users.find((user) => {
return user === username
})
if (userNotLogged) {
room.users.push(username)
}
}
// API Routes
router.get('/rooms', function(req, res) {
const rooms = database.map((room) => {
return {name: room.name, id: room.id}
})
console.log('Response:',rooms)
res.json(rooms)
})
router.get('/rooms/:roomId', function(req, res) {
room = findRoom(req.params.roomId)
if (room.error) {
console.log('Response:',room)
res.json(room)
} else {
console.log('Response:',{name: room.name, id: room.id, users: room.users})
res.json({name: room.name, id: room.id, users: room.users})
}
})
router.route('/rooms/:roomId/messages')
.get(function(req, res) {
room = findRoom(req.params.roomId)
if (room.error) {
console.log('Response:',room)
res.json(room)
} else {
console.log('Response:',room.messages)
res.json(room.messages)
}
})
.post(function(req, res) {
room = findRoom(req.params.roomId)
if (room.error) {
console.log('Response:',room)
res.json(room)
} else if (!req.body.name || !req.body.message) {
console.log(req);
console.log('Response:',{error: 'request missing name or message'})
res.json({error: 'request missing name or message'})
} else {
logUser(room, req.body.name)
const reaction = req.body.reaction || null
const messageObj = { name: req.body.name, message: req.body.message, id: shortid.generate(), reaction }
room.messages.push(messageObj)
console.log('Response:',{message: 'OK!'})
res.json(messageObj)
}
})
app.use('/api', router)
app.listen(port)
console.log(`API running at localhost:${port}/api`)