Skip to content

Instantly share code, notes, and snippets.

@AlenaNik
Last active June 19, 2019 10:23
Show Gist options
  • Save AlenaNik/6a460779f0af3f600bd30a64a51d7cd4 to your computer and use it in GitHub Desktop.
Save AlenaNik/6a460779f0af3f600bd30a64a51d7cd4 to your computer and use it in GitHub Desktop.
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);
});
@sdesalas
Copy link

sdesalas commented Jun 19, 2019

Hi Alena, dont forget you need to do an INSERT in the database for this to work (Express 3 - POST method and inserting data).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment