2.0.0 • Published 5 years ago

@leafgard/nodegard v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Key Features

  • Ability to choose between single and multi-core applications
  • Works with separated modules
  • Globally declared modules for inter-module communication (Ex: WebServer -> Database)
  • .ENV configuration ready
  • Pre-installed ExpressJS webserver
  • Loading, preparing and running phases
  • Easy-to-adapt modules

Installation

# Installs the NodeGard init CLI
$ npm i -g @leafgard/nodegard
# Creates a NodeGard project named "awesomeproject". Just follow the further instructions !
$ nodegard -i awesomeproject
# Your turn to play ! :)
$ cd awesomeproject

Updating package.json

You can update the package.json with your own information if you need, for example:

+ "repository": {
+   "type": "git",
+   "url": "git+https://github.com/yourName/yourRepository.git"
+  },
-  "license": "UNLICENSED",
+  "license": "YOUR LICENSE",
+  "bugs": {
+   "url": "https://github.com/yourName/yourRepository/issues"
+  },
+ "homepage": "https://github.com/yourName/yourRepository#readme",

Usage

Configuring the application in config.js

Adding a module to the application:

By default, the application runs using the built-in WebServer.

/**
 * All the modules should be declared here.
 * NB: Application won't run if empty !
 * 
 * WebServer is already declared here.
 * It's the built-in webserver provided by the framework.
 */
  module.exports.Modules = {
    'WebServer': require('./src/Modules/WebServer')
  }

You just have to add your own modules like this:

  module.exports.Modules = {
-    'WebServer': require('./src/Modules/WebServer')
+    'WebServer': require('./src/Modules/WebServer'),
+    'myModule': require('./src/Modules/MyModule')
  }

myModule is the keyname you can call throughout the whole application from all of your modules.

For example, if you have a Database module and you want to call one of its functions, you can just:

Database.callMyFunction() from anywhere in the app! And it will work, just like that!

require('./src/Modules/..') is the directory of your modules folder.

Single/Multi-Core applications

By default, applications will only run on a single core.

/**
 * Ability to run on multiple cores:
 * true = Uses multiple cores
 * false = Uses only one core
 */
  module.exports.useMultipleCores = false

In order to allow your application to make use of multiple cores, you must set module.exports.useMultipleCores to true.

Creating a module

This framework is based on modules.

All of your modules should be located in ./src/Modules/YourModule/index.js (Each module has its own directory).

All modules should have this syntax:

  module.exports = new ( class yourModule {

    /**
     * (Requires the packages and) Instantiate the main components and variables
     */
    constructor() {
      const express = require('express')
      this.app = express()
      this.webPort = process.env.WEB_PORT || 3000
      ...
    }

    /**
     * Prepares the module to be run (Settings, routes, etc..)
     * @returns {Promise} Resolve = module prepared, reject = problem while preparing module
     */
    prepare() {
      return new Promise((resolve, reject) => {
        this.app.get('/', (req, res) => {
          res.send('Hello World!')
        })
        ...
        resolve()
      })
    }

    /**
     * Runs the module (Listening, etc..)
     */
    run() {
      this.app.listen(this.webPort, () => {
        console.log(`Example app listening on port ${this.webPort}!`)
      })
      ...
    }

  } )

You absolutely need the prepare() and run() functions, and the prepare() function must return a Promise!

If there is a problem during prepare(), return reject, or else return resolve.

This prevents the application from running without everything working properly.

Testing

Create tests (using JEST) in ./tests and run:

$ npm test

Contribution

  • Fork the repository, use the development branch and please create pull requests to contribute to this project.
  • Follow the same coding style as used in the project. Pay attention to the usage of tabs, spaces, newlines and brackets. Try to copy the aesthetics as best as you can.
  • Write good commit messages, explain what your patch does, and why it is needed.
  • Keep it simple: Any patch that changes a lot of code or is difficult to understand should be discussed before you put in the effort.

Built With

  • dotenv^6.1.0 - Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env
  • express^4.16.4 - Fast, unopinionated, minimalist web framework for node
  • jest^23.6.0 - Delightful JavaScript Testing

Members

  • Yann SEGET - Main author - dev@leafgard.fr
  • Jonah WRIGHT - English-Language checker - jonah.wright@viacesi.fr

https://github.com/Leafgard/NodeGard

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

2.0.0

5 years ago

1.0.0

6 years ago