Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ralexs0096/fd968e466afebc8fc4be6ac1c43f35a1 to your computer and use it in GitHub Desktop.
Save Ralexs0096/fd968e466afebc8fc4be6ac1c43f35a1 to your computer and use it in GitHub Desktop.
Node server - class model
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