app.use(express.json())
do?
/:id
mean in a route?
request.params.id
PUT
and PATCH
?
PATCH
updates only the information that is passedPUT
updates all the information at oncedefault: <defaultValue>
to the schema500
error status code mean?
200
and a status 201
?
npm i
npm i express mongoose
express
mongoose
npm i --save-dev dotenv nodemon
dotenv
allows us to pull environmental variables from a .env
filenodemon
allows us to update our server without needing to restart each timepackage.json
, change devStart
to "devStart": "nodemon server.js"
server.js
file.env
file.gitignore
.env
node_modules
server.js
:
express
const express = require('express');
app
variable to configure the server
const app = express();
listen
method to app
to tell it what port to listen to
app.listen(3000, () => console.log('Server Started'))
mongoose
const mongoose = require('mongoose');
mongoose
to database
mongoose.connect('mongodb://<dbURL>, { useNewUrlParser: true })
db
variable to handle events with the database
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('Connected to database'))
DATABASE_URL
in .env
file equal to your database URL in mongoose.connect
DATABASE_URL=://mongodb://<dbURL>
server.js
change dbURL
to process.env.DATABASE_URL
.env
variables
require('dotenv').config()
app.use(express.json())
routes
folderconst <routeName> = require('./routes/<endpointName>')
()
\
before each ()
/\(206\)-123
selects (206)
from a list of phone numbersOR
criteria with REGEX
|
is the OR
equivalent
OR
criteria in ()
if there is an additional pattern to find after your conditional patterns/<pattern1>|<pattern2>/gm
Create
new booksRead
existing books’ dataUpdate
existing books statusDelete
existing books from DBUpdate
app.post('/books', postBooks)
Create function to post the the books
async function postBooks(req, res, next) {
try{
await Book.create(req.body);
} catch(err) {
next(err);
}
}
Add middleware
DELETE
/:id
parameter to the delete route so that it knows which book to delete
id
can be accessed with req.params.id
.findByIdAndRemove()
async function deleteBooks(req, res, next) {
try{
away Book.findByIdAndDelete(req.params.id);
res.send('Book deleted');
} catch(err) {
next(err);
}
}