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
20 changes: 14 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true,
"jest": true
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": "latest"
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 13,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
}
}
118 changes: 118 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,121 @@ typings/

# Mac files
.DS_Store
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
.env.production

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Your assignment page on Canvas should contain instructions for submitting this p

Use Node.js and Express to build an API that performs CRUD operations on users.

- Add a `server` script to the `package.json` that runs the API using `nodemon`.
- Add a `server` script to the `package.json` that runs the API using `nodemon`. - ok

### Write endpoints

Expand Down
136 changes: 135 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,137 @@
// BUILD YOUR SERVER HERE
// IMPORTS
const express = require("express")
const User = require("./users/model")

module.exports = {}; // EXPORT YOUR SERVER instead of {}
// INSTANCE OF EXPRESS
const server = express()

// GLOBAL MIDDLEWARE
server.use(express.json())

// ENDPOINTS
// POST
server.post('/api/users', (req, res)=>{
const user = req.body;
if (!user.name || !user.bio){
res.status(400).json({
message: "Please provide name and bio for the user",
})
}else{
User.insert(user)
.then(newUser => {
res.status(201).json(newUser)
})
.catch(err => {
res.status(500).json({
message: 'There was an error while saving the user to the database',
err: err.message,
stack: err.stack,
})
})
}

})


// GET USERS
server.get('/api/users', (req, res)=>{
User.find()
.then(users => {
res.json(users)
})

.catch(err => {
res.status(500).json({
message: "The users information could not be retrieved",
err: err.message,
stack: err.stack,
})
})
})


// GET USER ID
server.get('/api/users/:id', (req, res)=>{
User.findById(req.params.id)
.then(user => {
if (!user) {
res.status(404).json({
message: "The user with the specified ID does not exist",
})
}
res.json(user)
})

.catch(err => {
res.status(500).json({
message: "The user information could not be retrieved",
err: err.message,
stack: err.stack,
})
})
})

// DELETE
server.delete('/api/users/:id', async (req,res)=>{
try {
const potentialUser = await User.findById(req.params.id)
if(!potentialUser){
res.status(404).json({
message: "The user with the specified ID does not exist",
})
}else{
const deletedUser = await User.remove(potentialUser.id)
res.status(200).json(deletedUser)
}
}catch (err){
res.status(500).json({
message: "The user could not be removed",
err: err.message,
stack: err.stack,
})
}
})

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

}

}catch(err){
res.status(500).json({
message: "The user information could not be modified",
err: err.message,
stack: err.stack,
})
}
})

// ENDPOINTS
// Test
server.use('*', (req,res)=>{
res.status(404).json({
message: 'not found'
})
})




// EXPORT
module.exports = server; // EXPORT YOUR SERVER instead of {}
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ const server = require('./api/server');
const port = 9000;

// START YOUR SERVER HERE
// console.log("Why Hello!")
server.listen(port, ()=>{
console.log("Server running on port 9000")
})
Loading