Last active
December 7, 2021 13:36
-
-
Save luojiyin1987/cb8873ca492c1e68364865315be33c7f to your computer and use it in GitHub Desktop.
fcc 运动追踪器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // server.js | |
| // where your node app starts | |
| // init project | |
| var express = require('express'); | |
| const bodyParser = require('body-parser'); | |
| var app = express(); | |
| app.use(bodyParser.urlencoded({ extended: false })); | |
| app.use(bodyParser.json()); | |
| const mongoose = require("mongoose"); | |
| const { Schema } = mongoose; | |
| const mongoUrl = process.env['mongoURL']; | |
| mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }); | |
| const userSchema = new mongoose.Schema({ | |
| username: { type: String, required: true }, | |
| log: [] | |
| }) | |
| const Exerice = mongoose.model('Exerice', exericeSchema); | |
| const User = mongoose.model('User', userSchema); | |
| // enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) | |
| // so that your API is remotely testable by FCC | |
| var cors = require('cors'); | |
| app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204 | |
| // http://expressjs.com/en/starter/static-files.html | |
| app.use(express.static('public')); | |
| // http://expressjs.com/en/starter/basic-routing.html | |
| app.get("/", function (req, res) { | |
| res.sendFile(__dirname + '/views/index.html'); | |
| }); | |
| // your first API endpoint... | |
| app.post('/api/users', (req, res) => { | |
| // console.log('users', req.body); | |
| const username = req.body.username; | |
| const newUser = new User({ username }); | |
| newUser.save((error, savedUser) => { | |
| if (!error) { | |
| const obj = {}; | |
| obj['username'] = savedUser.username; | |
| obj['_id'] = savedUser._id; | |
| res.json(obj) | |
| } else { | |
| res.json(error) | |
| } | |
| }) | |
| }) | |
| app.post('/api/users/:_id/exercises', (req, res) => { | |
| let _id = req.params._id | |
| let date | |
| if (!req.body.date) { | |
| date = new Date().toDateString(); | |
| } else { | |
| date = new Date(req.body.date).toDateString(); | |
| } | |
| const newExerice = { | |
| description: req.body.description, | |
| duration: Number(req.body.duration), | |
| date | |
| } | |
| User.findOneAndUpdate({ _id: _id }, { $push: { log: newExerice } }, { new: true }, (error, savedExerice) => { | |
| if (!error) { | |
| console.log("savedExerice", savedExerice) | |
| const { _id, username } = savedExerice | |
| const { description, duration, date } = newExerice | |
| const obj = { _id, username, description, duration, date }; | |
| res.json(obj) | |
| } else { | |
| res.json(error); | |
| } | |
| }) | |
| }) | |
| app.get('/api/users', (req, res) => { | |
| User.find({}, (error, arrayOfUsers) => { | |
| res.json(arrayOfUsers) | |
| }) | |
| }) | |
| app.get('/api/users/:id/logs', (req, res) => { | |
| const _id = req.params.id; | |
| const obj = {}; | |
| User.findOne({ _id }, { "__v": 0 }, (error, userData) => { | |
| obj.username = userData.username; | |
| obj._id = _id; | |
| obj.count = userData.log.length; | |
| let fromDate = new Date() | |
| let toDate = new Date() | |
| console.log("query", req.query); | |
| if (req.query.limit) { | |
| userData.log = userData.log.slice(0, req.query.limit) | |
| } | |
| if (req.query.from || req.query.to) { | |
| if (!!req.query.from) { | |
| fromDate = new Date(req.query.from) | |
| } | |
| if (!!req.query.to) { | |
| toDate = new Date(req.query.to) | |
| } | |
| fromDate = fromDate.getTime(); | |
| toDate = toDate.getTime() | |
| userData.log = userData.log.filter((log) => { | |
| let exciseDate = new Date(log.date).getTime(); | |
| return exciseDate >= fromDate && exciseDate <= toDate | |
| }) | |
| } | |
| obj.log = userData.log | |
| res.json(obj) | |
| }) | |
| }) | |
| // listen for requests :) | |
| var listener = app.listen(process.env.PORT, function () { | |
| console.log('Your app is listening on port ' + listener.address().port); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment