-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (98 loc) · 3.03 KB
/
server.js
File metadata and controls
109 lines (98 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
const {addNewUser,getAllUsers,addNewExercise,fetchExerciseLog}=require('./db.js');
require("dotenv").config();
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
const listener =app.listen(process.env.PORT || 3000, ()=> {
console.log("app is listening at "+ listener.address().port);
});
// open the index.html when client side hit the root end of API
app.use(express.static('public'));
app.get('/', (req, res)=>{
res.sendFile(__dirname + '/views/index.html');
});
//body-parser
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//connect to mongodb
const mongoose = require("mongoose");
const db=mongoose.connect(process.env.uri,(err)=>{
if(err) console.error(err);
console.log("connected to mdb");
});
//crate a new user after getiing data from client side form
app.post('/api/users', async function(req,res,next){
//console.log(req.body.username);// body: [Object: null prototype] { username: 'username' }
const userName = req.body.username;
if(!userName || userName.length===0){
return res.json({error:"invalid username"});
}else{
try {
const newUser =await addNewUser(userName);;
console.log(newUser);
res.json({
username:newUser.username,
_id: newUser._id,
})
} catch (e) {
console.error(e);
}
}
})
//get a list of all users
app.get('/api/users',async(req,res,next)=>{
try {
const allUsers=await getAllUsers();
res.json(allUsers);
} catch (e) {
console.log(e);
}
})
//create exercise for an existing user
app.post('/api/users/:_id/exercises', async function(req,res,next){
const userId = req.params._id || req.body._id;
const todaysDate=new Date();
const yyyy = todaysDate.getFullYear();
const m = todaysDate.getMonth()+1;
const d = todaysDate.getDate();
const dateStr = yyyy + "-" + (m<=9 ? '0' +m:m) + "-" + (d<=9? '0' +d:d);
const datePartOnly = req.body.date ? req.body.date:dateStr;
try {
const newExercise = await addNewExercise({
id:userId,
description:req.body.description,
duration:req.body.duration,
date:datePartOnly
});
if(newExercise){
res.json(newExercise);
}else{
console.log(newExercise);
res.end();
}
} catch (e) {
console.error(e);
}
});
//retrieve log of all exercise of a user
app.get('/api/users/:_id/logs', async function(req,res, next){
const userId=req.params["_id"];
const {from, to, limit} = req.query;
const objToProcess ={
userId:userId,
fromDate:from,
toDate:to,
limit:limit
};
try {
const exerciseLog = await fetchExerciseLog(objToProcess);
if(exerciseLog){
res.json(exerciseLog);
}else{
console.log(exerciseLog);
res.end();
}
} catch (e) {
console.error(e);
}
})