Prepare API
This commit is contained in:
parent
5e200da66f
commit
a35e1bf093
|
@ -1,20 +1,51 @@
|
|||
const express = require("express");
|
||||
const path = require("path");
|
||||
const express = require("express")
|
||||
const path = require("path")
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const db = require("./models")
|
||||
const Airport = db['Airport']
|
||||
const Flight = db['Flight']
|
||||
|
||||
const app = express();
|
||||
db.sequelize.sync().then(() => console.log("DB synced."))
|
||||
|
||||
app.use(express.static(path.resolve(__dirname, '../client/build')));
|
||||
const PORT = process.env.PORT || 3001
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(express.static(path.resolve(__dirname, '../client/build')))
|
||||
app.use(express.json())
|
||||
|
||||
app.get("/api", (req, res) => {
|
||||
res.json({ message: "Hello from server!" });
|
||||
});
|
||||
res.json({ message: "Hello from server!" })
|
||||
})
|
||||
|
||||
app.get("/api/airports", (req, res) => {
|
||||
res.json(Airport.findAll())
|
||||
})
|
||||
|
||||
app.get("/api/airport/:id", (req, res) => {
|
||||
const id = req.params.id
|
||||
Airport.findByPk(id).then(data => {
|
||||
if (data)
|
||||
res.json(data)
|
||||
else
|
||||
res.status(404).json({message: `Cannot find airport with id=${id}`})
|
||||
})
|
||||
})
|
||||
|
||||
app.get("/api/flights/:dep_icao", (req, res) => {
|
||||
const dep_icao = req.params.dep_icao
|
||||
Flight.findAll({where: {dep_icao: dep_icao}}).then(data => {
|
||||
if (data)
|
||||
res.json(data)
|
||||
else
|
||||
res.status(500).json({message: `An error occurred while fetching flights.`})
|
||||
})
|
||||
})
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
|
||||
});
|
||||
res.sendFile(path.resolve(__dirname, '../client/build', req.path))
|
||||
})
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server listening on ${PORT}`);
|
||||
});
|
||||
console.log(`Server listening on ${PORT}`)
|
||||
})
|
Loading…
Reference in New Issue