Created
September 28, 2021 03:32
-
-
Save hieptl/205d457919a48579f3c24d14af1f3e82 to your computer and use it in GitHub Desktop.
index.js - Node.js - Tinder Clone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const bodyParser = require("body-parser"); | |
const cors = require("cors"); | |
const express = require("express"); | |
const multer = require("multer"); | |
const mysql = require("mysql"); | |
const path = require("path"); | |
const PORT = process.env.PORT || 8080; | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(cors()); | |
app.use(express.static(path.join(__dirname, "public"))); | |
// create constants for the application. | |
const constants = { | |
matchRequestStatus: { | |
pending: 0, | |
accepted: 1, | |
rejected: -1, | |
}, | |
}; | |
// config multers. | |
const storage = multer.diskStorage({ | |
destination: function (req, file, cb) { | |
cb(null, "public/img"); | |
}, | |
filename: function (req, file, cb) { | |
cb(null, `${file.fieldname}-${Date.now()}.jpg`); | |
}, | |
}); | |
const upload = multer({ storage: storage }); | |
// create datbase connection | |
const dbConn = mysql.createConnection({ | |
host: process.env.DB_HOST || "", | |
user: process.env.DB_USER_NAME || "", | |
password: process.env.DB_USER_PASSWORD || "", | |
database: process.env.DB_NAME || "", | |
port: process.env.DB_PORT || "", | |
}); | |
dbConn.connect(function (err) { | |
if (err) { | |
console.log(err); | |
throw err; | |
} | |
console.log("Database was connected"); | |
require("./routes")({ app, dbConn, upload, constants }); | |
app.listen(PORT, () => { | |
console.log(`Server is listening on port ${PORT}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment