Skip to content

Commit

Permalink
Add routing
Browse files Browse the repository at this point in the history
  • Loading branch information
barcellos-pedro committed Oct 5, 2024
1 parent a24fdfa commit 6560c79
Show file tree
Hide file tree
Showing 23 changed files with 1,584 additions and 80 deletions.
6 changes: 5 additions & 1 deletion config/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import "dotenv/config"

const CONFIG = {
export const CONFIG = {
port: process.env.PORT,
appName: "Demo App",
database: {},
logger: {},
cache: {},
mailer: {},
}

export default function config({ key, defaultValue }) {
Expand Down
11 changes: 11 additions & 0 deletions controllers/HomeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Possible Actions
// Index | Show | Create | Store | Edit | Update | Destroy

export default class HomeController {
index(req, res) {
res.render("index", {
title: "Home",
author: "barcellos-pedro",
})
}
}
13 changes: 0 additions & 13 deletions controllers/home-controller.js

This file was deleted.

11 changes: 11 additions & 0 deletions controllers/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Possible Actions
// Index | Show | Create | Store | Edit | Update | Destroy

export default class TodosController {
index(req, res) {
res.render("todos/index", {
title: "Todos - Home",
tasks: [],
})
}
}
9 changes: 9 additions & 0 deletions core/View.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import hanblebars from "express-handlebars"

const extname = ".hbs"

const { engine } = hanblebars.create({ extname })

export default function setupViewEngine(setupFn) {
setupFn(extname, engine)
}
63 changes: 31 additions & 32 deletions core/app.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
import express from "express"
import config from "../config/config.js"
import setupViewEngine from "./View.js"
import Router from "../routes.js"

export default class App {
#instance

constructor() {
console.log("[Thane Stack] ☄️")

this.#instance = express()
this.port = config({ key: "port", defaultValue: 8080 })

this.start()
this.app = express()
this.router = Router
this.#bootstrap()
}

static new() {
return new this()
}

get app() {
return this.#instance
start(port = 800) {
this.port = config({ key: "port", defaultValue: port })
this.app.listen(this.port, () => this.startLog())
}

start() {
this.app.listen(this.port, () =>
console.log(`⚡Application running on http://localhost:${this.port}`)
)
startLog() {
console.log(`⚡Application running on http://localhost:${this.port}`)
}
}

// TODO: Register Middlewares

// app.use(express.static("public"));
// app.use(express.json());
// app.use(express.urlencoded({ extended: false }));

// ----------------------------------------------

// TODO: Set View Engine (Handlebars)
#initRoutes() {
this.router.registerRoutes(this.app)
}

// import hanblebars from "express-handlebars";
#initMiddlewares() {
this.app.use(express.static("public"))
this.app.use(express.json())
this.app.use(express.urlencoded({ extended: false }))
}

// const hbs = hanblebars.create({
// extname: ".hbs",
// helpers,
// });
#initViews() {
setupViewEngine((extname, engine) => {
this.app.engine(extname, engine)
this.app.set("view engine", extname)
this.app.set("views", "views")
})
}

// app.engine(".hbs", hbs.engine);
// app.set("view engine", ".hbs");
// app.set("views", "views");
#bootstrap() {
this.#initMiddlewares()
this.#initViews()
this.#initRoutes()
}
}
37 changes: 26 additions & 11 deletions core/router.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
// Router
export default class Router {
routes = []

// import express from "express";
static init() {
return new this()
}

// const router = express.Router();
get(path, [routeController, action]) {
this.#addRoute({
method: "GET",
controller: routeController,
path,
action,
})

// router.get("/", (req, res) => {
// res.render("index", {});
// });
return this
}

class Router {
static get(path, props){
const [controller, method] = props
registerRoutes(app) {
this.routes.forEach(({ path, action, controller }) => {
const routeController = new controller()
const handler = routeController[action]
app.use(path, handler)
})
}

}
}
#addRoute(routeProps) {
this.routes.push({ ...routeProps, middleware: "none" })
console.log("[Routes]\n", this.routes)
}
}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import App from "./core/app.js"

App.new().start()
Loading

0 comments on commit 6560c79

Please sign in to comment.