Transcript of https://www.youtube.com/watch?v=fgTGADljAeg
const express = require('express') const app = express() app.listen(3000, () => console.log('Server has started'))
const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect('mongodb://localhost/subscribers') app.listen(3000, () => console.log('Server has started'))
const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect('mongodb://localhost/subscribers', { userNewUrlParser: true}) app.listen(3000, () => console.log('Server has started'))
const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect('mongodb://localhost/subscribers', { userNewUrlParser: true}) const db = mongoose.connection db.on('error', (error) => console.error(error)) db.once('open', () => console.error('Connected to database')) app.listen(3000, () => console.log('Server has started'))Now when you run devStart again, you should see Connected to database
DATABASE_URL = mongodb://localhost/subscribers
const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect(process.env.DATABASE_URL, { userNewUrlParser: true}) const db = mongoose.connection db.on('error', (error) => console.error(error)) db.once('open', () => console.error('Connected to database')) app.listen(3000, () => console.log('Server has started'))
require('dotenv').config() const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect(process.env.DATABASE_URL, { userNewUrlParser: true}) const db = mongoose.connection db.on('error', (error) => console.error(error)) db.once('open', () => console.error('Connected to database')) app.listen(3000, () => console.log('Server has started'))
require('dotenv').config() const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect(process.env.DATABASE_URL, { userNewUrlParser: true}) const db = mongoose.connection db.on('error', (error) => console.error(error)) db.once('open', () => console.error('Connected to database')) const subscribersRouter = require ('./routes/subscribers') app.listen(3000, () => console.log('Server has started'))
require('dotenv').config() const express = require('express') const app = express() const mongoose = require('mongoose') mongoose.connect(process.env.DATABASE_URL, { userNewUrlParser: true}) const db = mongoose.connection db.on('error', (error) => console.error(error)) db.once('open', () => console.error('Connected to database')) app.use(express.json()) const subscribersRouter = require ('./routes/subscribers') app.use('/subscribers', subscribersRouter) app.listen(3000, () => console.log('Server has started'))
const express = require('express') const router = express.Router() module.exports = router
const express = require('express') const router = express.Router() // Getting all router.get('/', (req, res) => { res.send('Hello World') }) // Getting one router.get('/:id', (req, res) => { }) // Creating one router.post('/', (req, res) => { }) // Updating one router.patch('/:id', (req, res) => { }) // Deleting one router.delete('/:id', (req, res) => { }) module.exports = router
Send Request
GET http://localhost:3000/subscribers
res.send(req.params.id)
###
Send Request
GET http://localhost:3000/subscribers/12
Click Send Request and get 12 back.const mongoose = require('mongoose') const subscriberSchema = new mongoose.Schema({ name: { type: String, required: true }, subscriberToChannel:{ type: String, required: true }, subscribeDate: { type: Date, required: true, default: Date.now } }) module.exports = mongoose.model('Subscriber', subscriberSchema)and save.
const express = require('express') const router = express.Router() const Subscriber = require('../models/subscriber') //Getting all router.get('/', (req, res) => { try { const subscribers = await Subscriber.find() res.json(subscribers) } catch (err) { res.status(500).json({ message: err.message}) } }) // Getting one router.get('/:id', (req, res) => { res.send(req.params.id) }) // Creating one router.post('/', async (req, res) => { }) try { const newSubscriber = await subscriber.save() res.status(201).json(newSubscriber) } catch (err) { res.status(400).json({ message: err.message}) }) } }) // Updating one router.patch('/:id', (req, res) => { }) // Deleting one router.delete('/:id', (req, res) => { }) module.exports = router
// Creating One
router.post('/', async (req, res) => {
})
try {
const newSubscriber = await subscriber.save()
res.status(201).json(newSubscriber)
} catch (err) {
res.status(400).json({ message: err.message})
})
POST http://localhost:3000/subscribers Content-Type: application/json {must skip line} { "name": "Amazing Person", "subscribedToChannel": "Web Dev Simplified" }
async function getSubscriber(req, res, next){ try { subscriber = await Subscriber.findById(req.params.id) if (subscriber == null) { return.res.status(404).json({ message: 'Cannot find subscriber'}) } } catch (err) { return.res.status(500).json({ message: err.message}) } res.subscriber = subscriber next() } }
//Getting One
router.get('/:id', getSubscriber, (req, res) => {
res.send(res.subscriber.name)
// getting one router.get('/:id', getSubscriber, (req, res) => { res.send(req.subscribers.name) })
GET http://localhost:3000/subscribers) in order to get current subscribers.
GET http://localhost:3000/subscribers/{copied ID}
// Getting one
router.get('/:id', (req, res) => {
res.json(res.subscriber)
})
Now try this on the 2nd Send Request in route.rest.
// Deleting one
router.delete('/:id', getSubscriber, asyn(req, res) => {
try {
await res.subscriber.remove()
res.json({ message: 'Deleted subscriber'})
} catch (err) {
res.status(500).json({ message: err.message})
}
})
###
Send Request
DELETE http://localhost:3000/subscribers
{copied ID}
}
// Updating One
router.patch('/:id', getSubscriber, async (req, res) => {
if (req.body.name != null) {
res.subscriber.name = req.body.name
}
if (req.body.subscribedToChannel != null) {
res.subscriber.subscribedToChannel = req.body.subscribedToChannel
}
try {
const updatedSubscriber = await res.subscriber.save()
} catch (err) {
res.status(400).json({ message: err.message })
}
})
(Adds async as already inserted above.) ###Send RequestPATCH http://localhost:3000/subscribers/{copied ID} Content-Type: application/json {must skip line} { "name": "New name", }
Content-Type: application/json
{must skip line}
{
"name": "Amazing Person",
"subscribedToChannel": "Web Dev Simplified"
}
What Is REST?. From Web Dev Simplified.
JavaScript Async Await. From Web Dev Simplified.
JavaScript Promises In 10 Minutes. From Web Dev Simplified.
NPM Crash Course. From Traversy Media.
Node.js Tutorial For Absolute Beginners. From Traversy Media.
Getting Started: Mongoose. mongoosejs.com.
MongoDB Crash Course. From Traversy Media.