Skip to content

Instantly share code, notes, and snippets.

@AlenaNik
Last active May 23, 2019 14:10
Show Gist options
  • Save AlenaNik/60375fd640d45148174f49efcbd1cf41 to your computer and use it in GitHub Desktop.
Save AlenaNik/60375fd640d45148174f49efcbd1cf41 to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
const port = 300;
const cors = require('cors');
app.use(cors());
app.use(express.json());
const movies = [
{ id : 1, movie : 'Ghost'},
{ id : 2, movie : 'Eternal Sunshine of the Spotless Mind'},
{ id : 3, movie : 'La Gran Belleza'},
{ id : 4, movie : '28 Days Later'}
];
app.get('/', (req, res) =>{
res.send('hello')
} )
app.get('/api/movies', (req, res) => {
res.send(movies)
});
app.get('/api/employee', (req, res) => {
res.send(movies);
res.status(304);
});
app.get(`/api/employee`, (req, res) => {
console.log("req.query", req.query);
if (!req.query.name) res.status(404).send(`Employee not found ${employee}`);
else {
res.sendStatus(304);
}
});
app.get('/api/movie/:id', (req, res) => {
const classmate = movies.find(target => target.id === parseInt(req.params.id));
res.send(movies)
});
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);
});
app.listen(port, () => console.log(`The server is running on ${port}` ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment