-
-
Save Chojiu15/e15aaa5f0a1d9408b8dd689a4f284414 to your computer and use it in GitHub Desktop.
indexWorldPop
This file contains hidden or 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 = 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