Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brittany morris #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
8 changes: 5 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"node": true
"es2021": true
},
"extends": "eslint:recommended",
"extends": [
"standard"
],
"parserOptions": {
"ecmaVersion": 12
},
Expand Down
75 changes: 74 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
// BUILD YOUR SERVER HERE
const express = require('express');
const server = express();
const users = require('./users/model')

module.exports = {}; // EXPORT YOUR SERVER instead of {}
//Creates a user using the information sent inside the `request body`. |
server.post('/api/users',(req, res) => {
users.insert(req.params.name, req.params.bio)
.then(user => {
res.status(201).json(user)
})
.catch(err => {
console.log(err)
res.status(500).json({ message: "Please provide name and bio for the user" })
})
});

// Returns an array users.
server.get('/api/users',(req,res) => {
users.find()
.then(users => {
res.status(200).json(users)
})
.catch(err => {
res.status(500).json({ message: "The users information could not be retrieved"})
})
});

//Returns the user object with the specified id
server.get('/api/users/:id', (req, res) => {
users.findById(req.params.id)
.then(user => {
if (user) {
res.status(200).json(user)
} else {
res.status(404).json({ message: "The user with the specified ID does not exist"})
}
})
.catch(err => {
console.log(err)
res.status(500).json({ message: err.message })
})
})

//Removes the user with the specified `id` and returns the deleted user.
server.delete('/api/users/:id', (req, res) => {
users.remove(req.params.id)
.then(user => {
if (user) {
res.status(200).json(user)
} else {
res.status(404).json({ message: "The user could not be removed"
})
}
})
.catch(err => {
console.log(err)
res.status(500).json({ message: err.message })
})
})

//Updates the user with the specified `id`
server.put('/api/users/:id', async (req, res) => {
const { id } = req.params
const { changes } = req.body
try{
const result = await users.update(id, changes)
res.status(200).json(result)
} catch {
console.log(err)
res.status(500).json({ message: "The user with the specified ID does not exist" })
}
})


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

const port = 5000;
const PORT = 5000;

// START YOUR SERVER HERE


server.get('/api/users', (req,res) => {
response.status(200).json()
})

server.listen(PORT, () => {
console.log('server is running')
})
Loading