-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.txt
76 lines (59 loc) · 1.87 KB
/
api.txt
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
/*
|--------------------------------------------------------------------------
| api.js -- server routes
|--------------------------------------------------------------------------
|
| This file defines the routes for your server.
|
*/
const express = require("express");
// import models so we can interact with the database
const User = require("./models/user");
// import authentication library
const auth = require("./auth");
// api endpoints: all these paths will be prefixed with "/api/"
const router = express.Router();
//initialize socket
const socketManager = require("./server-socket");
router.post("/login", auth.login);
router.post("/logout", auth.logout);
router.get("/whoami", (req, res) => {
if (!req.user) {
// not logged in
return res.send({});
}
res.send(req.user);
});
router.post("/initsocket", (req, res) => {
// do nothing if user not logged in
if (req.user) socketManager.addUser(req.user, socketManager.getSocketFromSocketID(req.body.socketid));
res.send({});
});
// |------------------------------|
// | write your API methods below!|
// |------------------------------|
router.get("/user", (req, res) => {
User.findById(req.query.userId).then((user) => {
//console.log(user);
res.send(user);
});
});
router.post("/bio", auth.ensureLoggedIn, (req, res) => {
console.log(`Received a chat message from ${req.user.name}: ${req.body.value}`);
User.findById(req.user._id).then((user) => {
user.bio = req.body.value;
user.save();
})
})
router.post("/icon")
// router.get("/bio", auth.ensureLoggedIn, (req, res) => {
// User.findById(req.user._id).then((user) => {
// res.send(user.bio)
// })
// })
// anything else falls to this "not found" case
router.all("*", (req, res) => {
console.log(`API route not found: ${req.method} ${req.url}`);
res.status(404).send({ msg: "API route not found" });
});
module.exports = router;