Files
smarthome/api/app.js
2024-03-23 14:56:45 +03:00

64 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const mongoose = require("mongoose");
const configs = require("./configs");
const chalk = require("chalk");
const helmet = require("helmet");
const morgan = require("morgan");
const cors = require("cors");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(helmet());
// Logs
app.use(morgan("dev"));
app.use(cors());
// Connect Mongoose
mongoose
.connect(configs.mongodburl, {
autoIndex: true
})
.then(() => {
console.log(chalk.cyanBright.bold("Mongodb is live!"));
})
.catch((error) => {
console.log("Mongodb error: " + error.message);
});
// Statik dosyaların servis edilmesi
app.use(express.static("build"));
// React uygulamasının web arayüzüne erişim
//app.get("/react-ui", (req, res) => {
// res.sendFile(path.join(__dirname, "public", "index.html"));
//});
// Device Set Route
app.use("/api/get", require("./route/device.get.route"));
// Save Route
app.use("/api/save", require("./route/save.route"));
// Device Set Route
app.use("/api/set", require("./route/device.set.route"));
// Device Log Route
app.use("/api/device-log", require("./route/device.log.route"));
// 404 Not Found Endpoint
app.use((req, res) => {
res.status(404).json({ error: true, message: error.message });
});
// Root public route
app.use("*", (req, res) => {
// res.sendFile("public/", { root: __dirname });
res.sendStatus(400);
});
// Server Listening
server.listen(configs.port, () => {
console.log(chalk.yellowBright.bold.red("Listening: " + configs.port));
});
// Error Handling
app.on("eror", (error) => {
console.log(error);
});