0.5.132 • Published 4 years ago

dotweb v0.5.132

Weekly downloads
56
License
MIT
Repository
-
Last release
4 years ago

.web

ready to use web server components based on MVC \  

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install dotweb

Fast development and ready to use

you should write a few lines to set up your server and continue with writing your model and controllers. dotweb detect your paths and connect all source codes together. \  

  • not necessary to build any router
  • not necessary to require files
  • not necessary to know about web servers
  • not necessary to understand what I say \  

Just make these 3 files to start:

\ MyApp/app.js

const { Application } = require("dotweb");

// provide an application initializer
const application = new Application("MyApp");

// initialize necessary services
application.start.redis();
application.start.mongoose("mongodb://localhost:27017/MyApp");
application.start.express();
application.start.http(8000);

// initialize all models dynamically
application.external("models");

// initialize all controllers dynamically
application.external("controllers");

\ MyApp/models/record.js

const { ModelMongoose } = require("dotweb");
const { Schema } = require("mongoose");

module.exports = class extends ModelMongoose {
  // build a new mongoose schema
  schema() {
    return new Schema({
      name: { type: String, required: true },
      number: String,
      address: String,
    });
  }
};

\ MyApp/controllers/api/record.js

const { Controller } = require("dotweb");

module.exports = class extends Controller {
  // handle all post requests
  post(params) {
    const { record } = this.application.models;
    return record(params).save();
  }
  // handle all get requests
  get() {
    const { record } = this.application.models;
    return record.find();
  }
};

do not need to restart your node service on file changes.

\  

Full Package is Available Now

start any service jut by one application.start command:

\  

start redis cache service:

application.start.redis();

\  

start mongoose database service:

application.start.mongoose(mongoURL);

mongoURL: mongodb connection link.

\  

start jsonwebtoken authorization service:

application.start.jsonwebtoken(secret);

secret: jsonwebtoken passphrase or secret.

\  

start express web service:

application.start.express(build, messages);

build: server build will return in any api calls.

messages: server error messages.

default massages are:

const messages = {
  0: "successfully respond",
  10: "no response from server",
  11: "unknown error",
  12: "wrong api address",
  13: "method not found",
  14: "access denied",
  15: "no response delivered in request deadline",
  16: "internal server error",
  17: "authorization failed",
  18: "fail to handle too many requests",
  19: "license expired",
  20: "controller not found",
  21: "no response",
  22: "no response from controller",
  23: "wrong api version",
};

\  

start http listener service:

application.start.http(port);

port: listening port.

\  

start https listener service:

application.start.https(options, port);

options: https need some options to start.

const fs = require("fs");

const options = {
  key: fs.readFileSync("privkey.pem"),
  cert: fs.readFileSync("cert.pem"),
  passphrase: "haShSEcrEt",
};

port: listening port.

\  

start firebase messaging service:

application.start.firebase(credential, databaseURL);

credential: firebase account credential.

databaseURL: firebase account database url.

\  

start socket.io service:

application.start.socket(build, messages);

build: server build will return in any api calls.

messages: server error messages.

default massages are:

const messages = {
  0: "successfully respond",
  10: "no response from server",
  11: "unknown error",
  12: "wrong api address",
  13: "method not found",
  14: "access denied",
  15: "no response delivered in request deadline",
  16: "internal server error",
  17: "authorization failed",
  18: "fail to handle too many requests",
  19: "license expired",
  20: "controller not found",
  21: "no response",
  22: "no response from controller",
  23: "wrong api version",
};

\  

Extra Features

\ use cache in controllers

const { Controller, cacheState } = require("dotweb");

module.exports = class extends Controller {
  override() {
    return {
      cacheState: cacheState.public,
      cacheExpire: 100,
    };
  }
  post(params, { id, ip, domain, method, role, userId, deviceId, login }) {
    return `
      <h2>This content will cache for 100 seconds</h2>
      <p>for exactly the same requests.</p>
    `;
  }
};

\ extra request info in controllers

const { Controller } = require("dotweb");

module.exports = class extends Controller {
  post(params, { id, ip, domain, method, role, userId, deviceId, login }) {
    return `
      <p>request id: ${id}</p>
      <p>client ip: ${ip}</p>
      <p>access domain: ${domain}</p>
      <p>request method: ${method}</p>
      <p>authorized user role: ${role}</p>
      <p>authorized user id: ${userId}</p>
      <p>authorized user device id: ${deviceId}</p>
      <p>authorized user login time stamp: ${login}</p>
    `;
  }
};

\ make custom error messages in controllers

const { Controller } = require("dotweb");

module.exports = class extends Controller {
  override() {
    return {
      messages: {
        101: "this is a test message",
      },
    };
  }
  post() {
    throw 101;
  }
};

\ use validator in models

const { ModelMongoose } = require("dotweb");
const Mongoose = require("mongoose");
const Joi = require("joi");
const Joigoose = require("joigoose")(Mongoose);

module.exports = class extends ModelMongoose {
  schema() {
    const joiSchema = Joi.object({
      name: Joi.string().required(),
      username: Joi.string().min(6).max(30).required(),
      password: Joi.string()
        .min(8)
        .max(30)
        .regex(/[a-zA-Z0-9]{3,30}/)
        .required(),
      mobile: Joi.string().required(),
      email: Joi.string().email().required(),
      created: Joi.date(),
    });
    const schema = Mongoose.Schema(Joigoose.convert(joiSchema));
    schema.path("created").default = new Date();
    schema.index({ username: 1 }, { unique: true });
    return schema;
  }
};

\   \ It's a beginning; more tools will available soon..

0.5.132

4 years ago

0.5.130

4 years ago

0.5.128

4 years ago

0.5.122

4 years ago

0.5.118

4 years ago

0.5.114

4 years ago

0.5.116

4 years ago

0.5.112

4 years ago

0.5.110

4 years ago

0.5.108

4 years ago

0.5.106

4 years ago

0.5.104

4 years ago

0.5.102

4 years ago

0.5.100

4 years ago

0.4.98

4 years ago

0.4.96

4 years ago

0.4.94

4 years ago

0.4.92

4 years ago

0.4.90

4 years ago

0.2.88

4 years ago

0.2.84

4 years ago

0.2.86

4 years ago

0.2.82

4 years ago

0.2.80

4 years ago

0.2.76

4 years ago

0.2.74

4 years ago

0.2.72

4 years ago

0.2.70

4 years ago

0.2.68

4 years ago

0.2.66

4 years ago

0.2.64

4 years ago

0.2.62

4 years ago

0.2.60

4 years ago

0.2.58

4 years ago

0.2.56

4 years ago

0.2.54

4 years ago

0.2.52

4 years ago

0.2.44

4 years ago

0.2.42

4 years ago

0.2.40

4 years ago

0.2.38

4 years ago

0.2.36

4 years ago

0.2.34

4 years ago

0.2.32

4 years ago

0.1.32

4 years ago

0.1.30

4 years ago

0.1.24

4 years ago

0.1.26

4 years ago

0.1.28

4 years ago

0.1.22

4 years ago

0.1.21

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.16

4 years ago

0.1.0

4 years ago

0.0.14

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago