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
89 changes: 87 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
// BUILD YOUR SERVER HERE
const express = require("express")
const server = express()
const db = require("./users/model")
server.use(express.json()) //< --must include to parse from JSON


server.get("/", (req, res) => {
res.json({ message: "Lambda 5000 webpt24. Yo, World!" })
})


server.get("/api/users", (req, res) => {
const users = db.find()
if (users){
res.json(users)
} else {
res.status(500).json({message: "The users information could not be retrieved",})
}
})


server.get("/api/users/:id", (req, res) => {
const user = db.findById(req.params.id)
if(user) {
res.json(user)
} else if (!user){
res.status(404).json({
message: "The user with the specified ID does not exist",
})
} else {
res.status(500).json({ message: "The user information could not be retrieved", })
}
})



server.post("/api/users/", (req, res) => {
const newUser = db.insert({

name: req.body.name,
bio: req.body.bio,
})
if (newUser){
res.status(201).json(newUser);
} else if ( !newUser.name && !newUser.bio) {
res.status(400).json({ message : "please provide name and bio for the user"})
} else {
res.status(500).json({ message: "There was an error while saving the user to the database"})
}

})


server.put("/api/users/:id", (req, res) =>{
const user = db.findById(req.params.id)
if (user) {
//update the user
const updatedUser = db.update(user.id, {
name: req.body.name || updatedUser.name,
bio: req.body.bio || updatedUser.bio,
})
res.json(updatedUser)
} else if (!user ){
res.status(404).json({
message: "The user with the specified ID does not exist",
})
} else {
res.status(500).json({message: "The user information could not be modified"})
}
})


server.delete("/api/users/:id", (req, res) =>{
const user = db.findById(req.params.id)
if (user) {
//update the user
db.remove(user.id)
res.status(204).end
} else if (!user){
res.status(404).json({
message: "The user with the specified ID does not exist",
})
} else {
res.status(500).json({message : "The user information could not be modified"})
}
})
module.exports = server

module.exports = {}; // EXPORT YOUR SERVER instead of {}
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const server = require('./api/server');

const port = 5000;

// START YOUR SERVER HERE


server.listen(port, () => {
console.log("server is go at localhost:5000")
})
Loading