Last active
June 19, 2019 10:23
-
-
Save AlenaNik/6a460779f0af3f600bd30a64a51d7cd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const port = 3000; | |
const connection = require('./conf'); | |
app.get('/api/movie', (req, res) => { | |
connection.query('SELECT * FROM movie', (err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send('Error retrieving movies'); | |
} else { | |
res.json(results); | |
} | |
}); | |
}); | |
app.get('/api/movies/names', (req, res) => { | |
connection.query('SELECT name FROM movie', (err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send('Error retrieving names of movies'); | |
} else { | |
res.json(results); | |
} | |
}); | |
}); | |
//// | |
app.post('/api/movie', (req, res) => { | |
const movie = { | |
id : movies.length +1, | |
name : req.body.name, | |
}; | |
movies.push(movie) | |
res.send(movie) | |
}); | |
///// | |
app.put('/api/movie/:id', (req, res) => { | |
const movie = movies.find(target => target.id === parseInt(req.params.id)); | |
movie.name = req.body.name; | |
res.send(movie); | |
}); | |
/// | |
app.delete('/api/movie/:id', (req, res) => { | |
const movie = movies.find(target => target.id === parseInt(req.params.id)); | |
const index = movies.indexOf(movie) | |
movies.splice(index, 1) | |
res.send(movie); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alena, dont forget you need to do an INSERT in the database for this to work (Express 3 - POST method and inserting data).