Files
smarthome/api/app.js
2024-07-16 21:52:31 +03:00

53 lines
1.3 KiB
JavaScript
Raw Permalink 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"));
// 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"));
// Root public route
// app.use("*", (req, res) => {
// 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);
});