Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const connection = require('./config');
const express = require("express");
const port = 3000;
const app = express();
connection.connect(err => {
if(err){
console.log('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
})
app.listen(port, () => {
console.log(`Server listen on ${port}`);
});
app.get("/", (req, res) => {
res.send("Welcome to my favourite movies list!");
});
app.get("/api/movies", (req, res) => {
connection.query("SELECT * FROM movies", (err, results) => {
if(err){
res.status(500).send("error retrieving datas");
}else{
res.status(200).json(results);
}
})
});
app.get("/api/movies/:id", (req, res) => {
connection.query(
"SELECT * FROM movies WHERE id=?",
[req.params.id],
(err, results) => {
if(err || results.length == 0){
res.status(500).send("error retrieving datas");
}else{
res.status(200).json(results);
}
})
});
app.get("/api/search", (req, res) => {
connection.query(
`SELECT * FROM movies WHERE duration <= ${req.query.maxDuration}`,
(err, results) => {
if(err){
res.status(500).send("error retrieving datas");
}else{
if(results.length > 0){
res.status(200).json(results);
}else{
res.status(404).send("No results for this duration");
}
}
})
});
app.get("/user", (req, res) => {
res.sendStatus(401);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment