59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
const express = require("express");
|
|
const errorHandler = require("./middleware/errorHandler");
|
|
const db = require("./config/dbConnectoin");
|
|
const fileUpload = require("express-fileupload");
|
|
const path = require("path");
|
|
const swiftly = require("swiftly-tools");
|
|
const cron = require("cron").CronJob;
|
|
const { expressSharp, FsAdapter } = require('express-sharp')
|
|
const { functions } = require("./controllers/Users");
|
|
const { autoFetch } = require('express-route-detector')
|
|
var cors = require('cors')
|
|
require("dotenv").config();
|
|
|
|
// Init database
|
|
db();
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 6060;
|
|
|
|
// Middleware
|
|
app.use(cors())
|
|
app.use(fileUpload());
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: false }));
|
|
app.use(swiftly.middleware.hsts)
|
|
app.use(swiftly.middleware.sign("Mr Swift"))
|
|
|
|
// The scheduled job
|
|
const job = new cron("0 0 * * *", function () {
|
|
functions.deleteUsers();
|
|
});
|
|
job.start();
|
|
|
|
// Routes
|
|
app.use(autoFetch(express))
|
|
app.use('/uploads', expressSharp({
|
|
imageAdapter: new FsAdapter(path.join(__dirname, 'uploads')),
|
|
}));
|
|
|
|
//Error handler
|
|
app.use(errorHandler.run);
|
|
|
|
//Init site data
|
|
Site.findOne({ profile: "main" }).then(async (data) => {
|
|
if (!data) {
|
|
await Site.create({
|
|
profile: "main",
|
|
});
|
|
}
|
|
}).catch(async (err) => {
|
|
await Site.create({
|
|
profile: "main",
|
|
});
|
|
})
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server runing on port: ${port}`);
|
|
});
|