74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
import express from "express";
|
|
import bodyParser from "body-parser";
|
|
import mongoose from "mongoose";
|
|
import cors from "cors";
|
|
import dotenv from "dotenv";
|
|
import multer from "multer";
|
|
import helmet from "helmet";
|
|
import morgan from "morgan";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
/* Routes */
|
|
import authRoutes from "./routes/auth.js";
|
|
import userRoutes from "./routes/users.js";
|
|
import postRoutes from "./routes/posts.js";
|
|
/* Controllers */
|
|
import { register } from "./controllers/auth.js";
|
|
import { createPost } from "./controllers/posts.js";
|
|
/* Middlewares */
|
|
import { verifyToken } from "./middlewares/auth.js";
|
|
|
|
import injectMockData from "./debug/injectMockData.js";
|
|
|
|
/**
|
|
* Config
|
|
* */
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
dotenv.config();
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use(helmet());
|
|
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
|
|
app.use(morgan("common"));
|
|
app.use(bodyParser.json({ limit: "30mb", extended: true }));
|
|
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
|
|
app.use(cors());
|
|
app.use("/assets", express.static(path.join(__dirname, "public/assets")));
|
|
|
|
/**
|
|
* Storage
|
|
* */
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, "public/assets");
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, file.originalname);
|
|
},
|
|
});
|
|
const upload = multer({ storage });
|
|
|
|
/**
|
|
* Routes
|
|
* */
|
|
app.post("/auth/register", upload.single("profilePicture"), register);
|
|
app.post("/posts", verifyToken, upload.single("contentPicture"), createPost);
|
|
app.use("/auth", authRoutes);
|
|
app.use("/users", userRoutes);
|
|
app.use("/posts", postRoutes);
|
|
|
|
/**
|
|
* Database
|
|
* */
|
|
const PORT = process.env.PORT || 5001;
|
|
mongoose
|
|
.connect(process.env.MONGO_URL, {
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
})
|
|
.then(() => {
|
|
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`));
|
|
})
|
|
.catch((error) => console.log(error.message));
|