added description

This commit is contained in:
mmacierzanka committed 2021-10-05 17:55:11 +02:00
1 parent e75d0400ef
commit 14ab8de789
1 file changed
+39 -38
+39 -38
View File
@@ -9,56 +9,57 @@ const events = require("./models/events");
const bodyParser = require("body-parser"); const bodyParser = require("body-parser");
app.use(bodyParser.json()); // to support JSON-encoded bodies app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use( app.use(
bodyParser.urlencoded({ bodyParser.urlencoded({
// to support URL-encoded bodies // to support URL-encoded bodies
extended: true, extended: true,
}) })
); );
app.use(express.static(path.join(__dirname, "client/build"))); app.use(express.static(path.join(__dirname, "client/build")));
app.use( app.use(
cors({ cors({
origin: "*", origin: "*",
}) })
); );
mongoose mongoose
.connect(process.env.MONGO_URI, { .connect(process.env.MONGO_URI, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
}) })
.then(() => console.log("connected")) .then(() => console.log("connected"))
.catch((err) => console.log(err)); .catch((err) => console.log(err));
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/client/build/index.html")); res.sendFile(path.join(__dirname + "/client/build/index.html"));
}); });
app.get("/api/getEvents", async (req, res) => { app.get("/api/getEvents", async (req, res) => {
try { try {
const event = await events.find(); const event = await events.find();
if (!event) throw Error("something went wrong"); if (!event) throw Error("something went wrong");
console.log(event); console.log(event);
res.status(200).json(event); res.status(200).json(event);
} catch (err) { } catch (err) {
res.status(400).json({ msg: err }); res.status(400).json({ msg: err });
} }
}); });
app.post("/api/createEvent", (req, res) => { app.post("/api/createEvent", (req, res) => {
let data = { let data = {
name: req.body.name, name: req.body.name,
type: req.body.type, type: req.body.type,
address: req.body.address, description: req.body.description,
time: req.body.time, address: req.body.address,
created: { time: Date.now() }, time: req.body.time,
lat: req.body.lat, created: { time: Date.now() },
lon: req.body.lon, lat: req.body.lat,
}; lon: req.body.lon,
events.create(data, (err) => { };
if (err) return res.status(400).json({ msg: err }); events.create(data, (err) => {
// saved! if (err) return res.status(400).json({ msg: err });
res.status(200).json("ok"); // saved!
}); res.status(200).json("ok");
});
}); });
app.use((req, res, next) => { app.use((req, res, next) => {
res.sendFile(path.join(__dirname + "/client/build/index.html")); res.sendFile(path.join(__dirname + "/client/build/index.html"));
}); });
app.listen(port, () => { app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`); console.log(`Example app listening at http://localhost:${port}`);
}); });