Skip to content

syntaxtrash/probable-octo-happiness

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

probable-octo-happiness

A small MVC framework built with Javascript Node.js Express.js

Table of contents

Folder structure

📦mvc
 ┣ 📂application
 ┃ ┣ 📂config
 ┃ ┃  ┣ 📜database.js
 ┃ ┃  ┗ 📜routes.js
 ┃ ┣ 📂controllers
 ┃ ┃  ┗📜main.js
 ┃ ┣ 📂models
 ┃ ┃  ┗📜connection.js
 ┃ ┗ 📂views
 ┃   ┗ 📂main
 ┃      ┣ 📂partials
 ┃      ┃  ┣ 📜header.ejs
 ┃      ┃  ┗ 📜footer.ejs
 ┃      ┗ 📜index.ejs
 ┣ 📂assets
 ┃ ┣ 📂scripts
 ┃ ┣ 📂css
 ┃ ┗ 📂svg
 ┣ 📜app.js
 ┣ 📜.gitignore
 ┣ 📜LICENSE
 ┣ 📜package-lock.json
 ┣ 📜package.json
 ┗ 📜README.md

Installation

  1. Install nodejs.
  2. Install nodemon.
npm install -g nodemon
  1. Clone the repository.
  2. After cloning the repository, type in your terminal.
cd probable-octo-happiness
npm install

This will install the following.

  • express
  • body-parser
  • ejs
  • mysql

Basic Usage

To start the server, type in your terminal.

nodemon app.js

Connecting to your database.

Navigate to application\config\database.js and put your database info.

  const DatabaseConnectionInfo = {
  host: "localhost",
  port: "3306",
  user: "root",
  password: "",
  connectionLimit: 50,
  database: "jsmysql",
  };

Creating a new route.

  • Navigate to application\config\routes.js
  • Import your controller
const MyController    = require("../controllers/MyController");
  • Add the new route
Routes.get("/myNewRoute", function(req,res){
   return new MyController(req,res).myFunction();
});

Creating a new controller

  • Navigate to application\controllers
  • Your controller and class name must be the same. Example: MyController.js
  • MyController.js class should look like:
class MyController{
   #req;
   #res;

   constructor(req,res){
       this.#req = req;
       this.#res = res;
   }


   myFunction = async function(){
       // your code here
   }
}

module.exports = MyController;

Creating a new model

  • Navigate to application\models
  • Your model and class name must be the same. Example: MyModel.js
  • Import database connection and mysql.
const  DatabaseConnection   = require("../config/database");
const Mysql                 = require("mysql");
  • MyModel.js class should look like:
const DatabaseConnection   = require("../config/database");
const Mysql                = require("mysql");

class MyModel{

   async myModelFunction() {
       // your code here
   }
}

module.exports = MyModel;

Creating a new view

  • Navigate to application\views
  • You may directly create a view inside the views folder, but I recommend to create a folder with the name of the controller then create the view inside the folder. See the folder structure. Example: MyController/MyView.ejs
  • MyView.ejs should look like:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>

Views with partials

  • MyView.ejs should look like:
<%- include("partials/header.ejs") %>
<main>
  <h1>Hello Universe!</h1>
</main>
<%- include("partials/footer.ejs") %>
  • inside partials\header.ejs
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="css/styles.css">
  <title><%= title%></title>
</head>
<body>
  • inside partials\footer.ejs
</body>
</html>

Features are to be added in the future.

Releases

No releases published

Packages

No packages published