Setting up database
This commit is contained in:
committed by
Andrew Trieu
parent
76f7c48c8c
commit
a65a0f206a
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
**/node_modules
|
||||
.vscode
|
||||
**/.env
|
||||
@@ -0,0 +1,53 @@
|
||||
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";
|
||||
|
||||
/**
|
||||
* 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 });
|
||||
|
||||
/**
|
||||
* 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));
|
||||
|
||||
Reference in New Issue
Block a user