Skip to content

Instantly share code, notes, and snippets.

@Chojiu15
Created September 21, 2021 09:53
Show Gist options
  • Save Chojiu15/e15aaa5f0a1d9408b8dd689a4f284414 to your computer and use it in GitHub Desktop.
Save Chojiu15/e15aaa5f0a1d9408b8dd689a4f284414 to your computer and use it in GitHub Desktop.
indexWorldPop
const express = require("express");
const app = express();
const PORT = 3002;
app.use(express.json());
require("dotenv").config;
const connection = require("./conf");
app.use(
express.urlencoded({
extented: true,
})
);
app.get("/api/populations", (req, res) => {
connection.query(`SELECT * FROM population_years `, (err, results) => {
if (err) throw err;
res.send(results);
});
});
app.get("/api/populations/:country", (req, res) => {
const country = req.params.country;
connection.query(
`SELECT * FROM population_years WHERE country = ?`,
country,
(err, results) => {
if (err) throw err;
res.send(results);
}
);
});
app.post("/api/populations", (req, res) => {
const formData = req.body;
const { country, population, year } = req.body;
connection.query(
`INSERT INTO population_years (country, population,year) VALUES(?, ? , ?) `,
[country, population, year],
(err, results) => {
if (err) throw err;
res.send(results);
}
);
});
app.delete("/api/populations/:country", (req, res) => {
const country = req.params.country;
connection.query(
`DELETE FROM population_years WHERE country = ?`,
country,
(err, results) => {
if (err) throw err;
res.send("Country deleted from the list");
}
);
});
app.put("/api/populations/:country", (req, res) => {
const formData = req.body;
const country = req.params.country;
connection.query(
`UPDATE population_years SET ? WHERE country = ?`,
[formData, country],
(err, results) => {
if (err) throw err;
res.send(results);
}
);
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment