dotweb

ready to use web server components based on MVC \  

Usage no npm install needed!

<script type="module">
  import dotweb from 'https://cdn.skypack.dev/dotweb';
</script>

README

.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..