Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions api/server.js

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
0 README.md → backend/README.md
100755 → 100644
File renamed without changes.
81 changes: 81 additions & 0 deletions backend/api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// BUILD YOUR SERVER HERE
const express = require("express");
const cors = require("cors");
const Data = require("./users/model.js")
const server = express();

server.use(express.json());
server.use(cors());
//!get
server.get("/api/users",async(req,res)=> {
try {
const users = await Data.find();
res.status(200).json(users);
} catch (err) {
res.status(500).json({ message: "The users information could not be retrieved" })
}
})
server.get("/api/users/:id",async(req,res)=> {
try {
const {id} = req.params;
const userById = await Data.findById(id);
if (!userById) {
res.status(404).json({ message: "The user with the specified ID does not exist" })
} else {
res.status(200).json(userById)
}
} catch {
res.status(500).json({ message: "The user could not be removed" })
}
})
//!get
//!post
server.post("/api/users",async(req,res)=> {
try {
const {name, bio} = req.body;
if (!name || !bio) {
res.status(400).json({ message: "Please provide name and bio for the user" });
} else {
const newUser = await Data.insert({name : name,bio :bio});
res.status(201).json(newUser)
}
} catch {
res.status(500).json({ message: "There was an error while saving the user to the database" })
}
})
//!post
//!delete
server.delete("/api/users/:id",async(req,res)=> {
try {
const {id} = req.params;
const deletedUser = await Data.remove(id);
if (!deletedUser) {
res.status(404).json({ message: "The user with the specified ID does not exist" });
} else {
res.status(200).json(deletedUser);
}
} catch {

}
})
//!delete
//!put
server.put("/api/users/:id",async(req,res)=> {
try {
const {id} = req.params;
const {name,bio} = req.body;
const updatedUser = await Data.update(id,{name : name, bio : bio});
if (!updatedUser) {
res.status(404).json({ message: "The user with the specified ID does not exist" });
} else if (!name || !bio) {
res.status(400).json({ message: "Please provide name and bio for the user" });
} else {
res.status(200).json(updatedUser);
}

} catch {
res.status(500).json({ message: "The user information could not be modified" })
}
})
//!put
module.exports = server; // EXPORT YOUR SERVER instead of {}
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const server = require('./api/server');

const port = 9000;

// START YOUR SERVER HERE
server.listen(port,()=> {
console.log(`server running on http://localhost:${port}`);
})
File renamed without changes.
Loading