qwert v1.1.9
Qwert
Node.js web application development framework built on top of Express
Installation
- You can use Qwert starter github repository
- clone this repository on your local disk
- run
npm install
from command line
- Or use npm repository
- run
npm install qwert
from command line in your project directory
- run
Configuration
example configuration:
const qwert = require("qwert");
const path = require("path");
qwert.config({
root_dir: path.resolve("./src"), // Required
controllers_dir: "./controllers", // optional, default is "controllers"
models_dir: "./models", // optional, default is "models"
middleware_dir: "./middleware", // optional, default is "middleware"
views: {
dir: "./views" // optional, default is "views"
},
port: 3000
});
qwert.boot();
According to this configuration, our directory structure will be
Qwert will scan these directories and read model, view, controller and middleware js files from there, You can read more about how to create these files below
Modules
require("qwert")
- read docsrequire("qwert/Controller")
- read docsrequire("qwert/ControllerGroup")
- read docsrequire("qwert/Middleware")
- read docsrequire("qwert/Model")
- read docs
Controller
Controllers must be located in controllers_dir directory ###How to create / Example create file in controllers directory, for example TestController.js Contents:
// file: controllers/TestController.js
const Controller = require("qwert/Controller");
module.exports = Controller(function($request, $response) {
// some controller logic
}, {
route: "/test"
});
this will create controller, that handles request on /test page Qwert injects services as controller function arguments by argument names, read about services
Parameters
Controller(fn, options)
- fn (required) - Function - controller function, arguments are services
- options (required) - Object - options of controller
- route (required) - String - route that will be handled by controller, this parameter is
- middleware - Array, String - middleware name or array of middleware names
- method - String - method that will be handled (GET, POST, PUT, PATCH etc...), default is GET
ControllerGroup
Controller groups must be located in controllers_dir directory
###How to create / Example create file in controllers directory, for example TestControllerGroup.js Contents:
// file: controllers/TestControllerGroup.js
const ControllerGroup = require("qwert/ControllerGroup");
var group = ControllerGroup({
route: "/home"
});
group.add(Controller(function($request, $response) {
// Some controller logic
}, {
route: "/main"
}));
module.exports = group;
This code will create ControllerGroup that handles requests on /home path Then added one controller which handles /home/main request, you can add as much controllers in group as needed
Parameters
ControllerGroup(options)
- options (required) - Object - Controller group options
- route (required) - String - base path of the controllers in this group
- middleware - Array, String - middleware or list of middleware for this group
Model
Models must be located in models_dir
How to create / Example
create file in models directory, for example TestModel.js
Contents:
// file: models/TestModel.js
const Model = require("qwert/Model");
module.exports = Model("test", function(args) {
this.getName = function() {
return "George";
}
});
this code creates model with name test model can be instantiated from middleware or controller by service $model Example code:
Controller(function($model, $param) {
var userModel = $model("user"); // user is the name of model
var user = userModel.getUser($param.get("id"));
// some logic
}, {
route: "/user/:id"
})
Parameters
Model(name, fn)
- name (required) - String - the name of model, must be unique among all models
- fn (required) - Function - model function, will be instantiated with new keyword
- arguments are services
##Middleware Middleware must be located in middleware_dir directory
##How to create / Example Create file in middleware directory, for example TestMiddleware.js
Contents:
// file: middleware/TestMiddleware.js
const Middleware = require("qwert/Middleware");
module.exports = Middleware("testMiddleware", function($response, $post, $next) {
if(!$post.has("id")) {
$response.json({
error: "You must provide id"
});
} else {
$next();
}
});
this code creates middleware with name testMiddleware testMiddleware can be used as middleware of Controller or ControllerGroup
Note: In case if you don't call $next function, you have to handle response
Parameters
Middleware(name, fn)
- name (required) String - the name of middleware, must be unique among middlewares
- fn (required) Function - middleware function
- arguments are services
##Service injection
Services
Services are used in controller, model and middleware functions Service is passed to the function by name Example
function Test($post, $response) { // Parameter sequence and length has no matter
$response.send($post.get("name"));
}
$request
- express request object see docs$response
- express response object see docs- example:
$response.send("Hello world");
- example:
$post
- POST data of the request- example 1:
$post.get("user_id"); // returns empty string if no user_id in post data
- example 2:
$post.all(); // returns object that contains all post data
- example 1:
$get
- GET data of the request$param
- Parameters of the router- for example, router string:
/user/:id
,$param.get("id")
will return the id
- example:
$get.get('user_id');
same as $post
- for example, router string:
$model
- Model provider$session
- Session handler service- example
var user = $model('User');
- example
$next
- used in Middleware, function that has to be called to continue request flow$upload
- Service for accepting uploaded files, files won't stored unless you accept them by this service, generally used in middleware- example
$upload('file1').then(next);
where file1 is form field and next is callback function
- example
$files
- files uploaded byContent-Type: multipart/form-data
and received by $upload service- example
var filename = $files.handler().get('file1').moveAutoName(__dirname+'../images/', true);
- example