Skip to content

Commit

Permalink
Adding home controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Wathfea committed Sep 3, 2024
1 parent 4c57b93 commit 082d16c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
13 changes: 3 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const loadRoutes = require('./loaders/routes.js');
const homeModule = require("./components/home/home.module");


const corsOptions = {
origin: '*',
Expand All @@ -17,16 +18,8 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(cors(corsOptions));
app.options('*', cors(corsOptions)); // Enable pre-flight
app.use('/', homeModule.router);

app.get('/', (req, res) => {
res.send('Hello World')
})

app.get('/about', (req, res) => {
res.send('About route 🎉 ')
})

loadRoutes(app);

const start = () => {
try {
Expand Down
20 changes: 20 additions & 0 deletions components/home/home.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class HomeController {
getHome = async (_, res) => {
try {
return res.status(200).send('Welcome to the Pullit API');
} catch (error) {
if (process.env.NODE_ENV === 'development') {
return res.status(500).json({
error: 'Internal Server Error',
'msg': error.message,
'stack': error.stack
});
} else {
return res.status(500).json({error: 'Internal Server Error'});
}
}
};

}

export default HomeController;
10 changes: 10 additions & 0 deletions components/home/home.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const HomeController = require('./home.controller');
const HomeRouter = require('./home.router');

const homeController = new HomeController();
const homeRouter = new HomeRouter(homeController);

export default {
router: homeRouter.router,
};

16 changes: 16 additions & 0 deletions components/home/home.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');

class HomeRouter {
constructor(homeController) {
this.homeController = homeController;
}

get router() {
const router = express.Router();
router.route('/').get(this.homeController.getHome);

return router;
}
}

export default HomeRouter;
7 changes: 0 additions & 7 deletions loaders/routes.js

This file was deleted.

0 comments on commit 082d16c

Please sign in to comment.