natural-utility
natural-utility
is a simple helper tool to get easily your work as a module loader, route middleware, plugin middleware and flash message.
Table of Contents
Get Started
Language Translate
Framework Support
Framework Support | globalModule | pluginMiddleware | flashMessage |
---|---|---|---|
Express | ✔️ | ✔️ | ✔️ |
Koa | ✔️ | ✔️ | ✔️ |
Hapi | ✔️ | ❌ | ❌ |
Fastify | ✔️ | ✔️ | ❌ |
Restify | ✔️ | ❌ | ❌ |
Sails | ✔️ | ❌ | ❌ |
Total | ✔️ | ❌ | ❌ |
Adonis | ✔️ | ❌ | ❌ |
Feathers | ✔️ | ❌ | ❌ |
Keystone | ✔️ | ❌ | ❌ |
Nest | ❌ | ❌ | ❌ |
LoopBack | ❌ | ❌ | ❌ |
Mean | ❌ | ❌ | ❌ |
Features
- Parallel Module Loader
- Parallel Route Middleware
- Parallel Plugin Middleware
- Flash Message
- Global Module Access
Installation
$ npm i natural-utility --save| OR$ yarn add natural-utility --save
Example Usage
// register all moduleconst natural = ;natural; // init all moduleconst app = ;const server = http; // init all routeconst indexRoute = ; // register all plugin middlewarenatural; // register template engineapp;app; // register all route midllewarenatural; // listening serverserver;
API Reference
naturalModule
-
globalModule ( inputs: [string...], modules: [string...] ) function method of
naturalModule
to register each given module without the need to rewriterequire
, then the module will be run as parallel, the module can also be accessed as a global in every file or route in many ways, which you haven't to re-register the same module as you want to use it, note: global module only supports writing format CommonJS as require.-
Before - example usage not using natural-utility
-
// register all moduleconst express = ;const app = ;const http = ;const server = http;// listening serverserver;
-
-
After - example usage using natural-utility
-
// register all moduleconst natural = ;natural;// init all moduleconst app = ;const server = http;// listening serverserver;
-
-
naturalRoute
-
routeMiddleware ( app, routes: [string...] ) function method of
naturalRoute
to register each given route, then the route will be run as parallel, which it haven't to rewriteapp.use
middleware many of times to register every each route.-
Before - example usage not using natural-utility
-
// register all moduleconst express = ;const app = ;const http = ;const server = http;// init all routeconst indexRoute = ;const usersRoute = ;// register all route middlewareapp;app;// listening serverserver;
-
-
After - example usage using natural-utility
-
// register all moduleconst natural = ;natural;// init all moduleconst app = ;const server = http;// init all routeconst indexRoute = ;const usersRoute = ;// register all route middlewarenatural;// listening serverserver;
-
-
naturalPlugin
-
pluginMiddleware ( app, plugins: [string...], options: {} ) method of the
naturalPlugin
function to register each given plugin, the plugin will be run as parallel, which it haven't to rewriteapp.use
middleware many of times to register every each plugin.-
Before - example usage not using natural-utility
-
// register all moduleconst express = ;const app = ;const http = ;const server = http;const bodyParser = ;const cookieParser = ;const logger = ;// init all routeconst indexRoute = ;const usersRoute = ;// register all plugin middlewareapp;app;app;// register all route middlewareapp;app;// listening serverserver;
-
-
After - example usage using natural-utility
-
// register all moduleconst natural = ;natural;// init all moduleconst app = ;const server = http;// init all routeconst indexRoute = ;const usersRoute = ;// register all plugin middlewarenatural;// register all route middlewarenatural;// listening serverserver;
-
-
naturalFlash
-
flashMessage ( ) function method of
naturalFlash
to display an error message or any message that might be displayed later, every requested time is made, note:cookie-parser
is not required now and flashMessage is now renamed to flashExpress, more info check here.-
Before - example usage not using natural-utility
-
//app.js// register all moduleconst express = ;const app = ;const http = ;const server = http;const flash = ;const session = ;//init all routeconst indexRoute = ;const usersRoute = ;// register all plugin middlewareapp;app;app;// register all route middlewareapp;app;// listening serverserver;
-
// routes/home.js// register all moduleconst express = ;const router = express;// setup route middlewarerouter;// export route middlewaremoduleexports = router;
-
<!-- views/home.ejs -->Natural<!-- result message --><%- messages() %>
-
-
After - example usage using natural-utility
-
// app.js// register all moduleconst natural = ;natural;// init all moduleconst app = ;const server = http;// init all routeconst indexRoute = ;const usersRoute = ;// register all plugin middlewarenatural;// register all route middlewarenatural;// listening serverserver;
-
// routes/home.js// register all moduleconst express = ;const router = express;// setup route middlewarerouter;// export route middlewaremoduleexports = router;
-
// routes/home.js// register all moduleconst express = ;const router = express;// setup route middlewarerouter;// export route middlewaremoduleexports = router;
-
<!-- views/home.ejs -->Natural<!-- sigle message --><%- messages() %>
-
<!-- views/home.ejs -->Natural<!-- multiptle message --><% if(messages()) { %><% messages().forEach((msg) => { %><%= msg %><%})%><%}%>
-
-
Working With Database
MongoDB
-
example usage using mongodb
-
// register all moduleconst natural = ;natural;// init collectionconst database = db;// init dataconst myData = name: "John Doe" age: 28 ;// insert data to databasedatabase;});
-
MySQL
-
example usage using mysql
-
// register all moduleconst natural = ;natural;// setup database connectionconst con = mysql;// init connectioncon;
-
PostgreeSQL
-
example usage using postgree sql
-
// register all moduleconst natural = ;natural;// setup database connectionconst client =user: 'root'host: 'localhost'database: 'testing'password: '123'port: 5432;// init connectionclient;// insert data to databaseconst query = `INSERT INTO users (name, age) VALUES ('john doe', 28)`;client;
-
Mongoose
-
example usage using mongoose
-
// register all moduleconst natural = ;natural;// setup database connectionmongoose;// setup schemaconst setSchema =name: Stringage: Number;// register schemaconst userSchema = setSchema;// insert data to databaseuserSchema;
-
Working With Framework
How To Test
natural-utility uses supertest
to mock http requests, thus testing no longer relies on the http server.
-
$ npm install$ npm test$ npm run test
Contributors
- Vikri Kurniawan - English Translator
- Restu Wahyu Saputra - Indonesian Translator