Created
May 31, 2022 07:24
-
-
Save Ralexs0096/fd968e466afebc8fc4be6ac1c43f35a1 to your computer and use it in GitHub Desktop.
Node server - class model
This file contains 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
const express = require('express'); | |
const {dbConnection} = require('../database/config'); | |
const cors = require('cors'); | |
class Server { | |
constructor(){ | |
this.app = express(); | |
this.port = process.env.PORT || 4001; | |
this.exampleRoute = "/api/example"; | |
this.connectDB(); | |
this.midlewares(); | |
this.routes(); | |
} | |
// *************** Methods *******************// | |
async connectDB() { | |
await dbConnection(); | |
} | |
midlewares() { | |
this.app.use(express.json()); | |
this.app.use(express.urlencoded({extended: true})); | |
this.app.use(cors()); | |
} | |
routes() { | |
this.app.use(this.exampleRoute, require('../routes/exampleRoute')); | |
} | |
listen() { | |
this.app.listen(this.port, () => console.log('Server running on Port', this.port)); | |
} | |
} | |
module.exports = Server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment