ready to use web server components
npm install dotweb
$ 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_
`javascript
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_
`javascript
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_
`javascript
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:
`javascript
application.start.redis();
`
\
start mongoose database service:
`javascript
application.start.mongoose(mongoURL);
`
_mongoURL_: mongodb connection link.
\
start jsonwebtoken authorization service:
`javascript
application.start.jsonwebtoken(secret);
`
_secret_: jsonwebtoken passphrase or secret.
\
start express web service:
`javascript
application.start.express(build, messages);
`
_build_: server build will return in any api calls.
_messages_: server error messages.
default massages are:
`javascript
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:
`javascript
application.start.http(port);
`
_port_: listening port.
\
start https listener service:
`javascript
application.start.https(options, port);
`
_options_: https need some options to start.
`javascript
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:
`javascript
application.start.firebase(credential, databaseURL);
`
_credential_: firebase account credential.
_databaseURL_: firebase account database url.
\
start socket.io service:
`javascript
application.start.socket(build, messages);
`
_build_: server build will return in any api calls.
_messages_: server error messages.
default massages are:
`javascript
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
`javascript
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
for exactly the same requests.
;
}
};
`
\
extra request info in controllers
`javascript
const { Controller } = require("dotweb");
module.exports = class extends Controller {
post(params, { id, ip, domain, method, role, userId, deviceId, login }) {
return
request id: ${id}
client ip: ${ip}
access domain: ${domain}
request method: ${method}
authorized user role: ${role}
authorized user id: ${userId}
authorized user device id: ${deviceId}
authorized user login time stamp: ${login}
;
}
};
`
\
make custom error messages in controllers
`javascript
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
`javascript
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;
}
};
``