Compare commits

...

2 Commits

Author SHA1 Message Date
0327b36031 Impl: creating users
Duplicate `usernames` allowed, user `_id` is unique and used to identify accounts
2023-03-29 01:56:48 +11:00
2c5e138e9a Scaffold 2023-03-28 16:31:44 +11:00
3 changed files with 1519 additions and 778 deletions

View File

@ -1,18 +1,56 @@
const express = require('express') const express = require("express");
const app = express() const app = express();
const cors = require('cors') const cors = require("cors");
require('dotenv').config() const mongoose = require("mongoose");
require("dotenv").config();
app.use(cors()) const PORT = process.env.PORT || 16749;
app.use(express.static('public')) const MONGODB_URI = process.env.MONGODB_URI || "";
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html') mongoose.connect(MONGODB_URI, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
}); });
const exerciseLogsSchema = new mongoose.Schema({
username: { type: String, required: true },
log: [
{
description: { type: String, required: true },
duration: { type: Number, required: true },
date: { type: String, required: true },
},
],
});
const exerciseLogs = mongoose.model("ExerciseLogs", exerciseLogsSchema);
app.use(cors());
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
app.post("/api/users", (req, res) => {
const username = req.body.username;
const exerciseLogsModel = new exerciseLogs({ username: username });
exerciseLogsModel
.save()
.then((user) => {
if (user) return res.json({ username: user.username, _id: user._id });
res.status(500);
res.json({ error: `Error creating user: ${username}` });
})
.catch((err) => {
res.status(500);
return res.json({
error: `Error type 2 while creating user: ${username}`,
});
});
});
const listener = app.listen(process.env.PORT || 3000, () => { const listener = app.listen(PORT, () => {
console.log('Your app is listening on port ' + listener.address().port) console.log("Your app is listening on port " + listener.address().port);
}) });

2196
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,23 @@
{ {
"name": "fcc-exercise-tracker", "name": "@radii-fcc-backend/fcc-exercise-tracker",
"version": "0.1.0", "version": "0.1.0",
"description": "A REST API project, part of Free Code Camp's curriculum", "description": "A REST API project, part of Free Code Camp's curriculum",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js" "start": "node index.js"
}, },
"dependencies": { "dependencies": {
"dotenv": "^8.2.0", "cors": "^2.8.5",
"express": "^4.16.4", "dotenv": "^8.2.0",
"cors": "^2.8.5" "express": "^4.16.4",
}, "mongoose": "^7.0.3"
"repository": { },
"url": "https://github.com/freeCodeCamp/boilerplate-project-exercisetracker" "repository": {
}, "url": "https://github.com/freeCodeCamp/boilerplate-project-exercisetracker"
"license": "MIT", },
"keywords": [ "license": "AGPL-3.0-or-later",
"node", "keywords": [
"express" "node",
] "express"
]
} }