@snipkode/stack
v5.1.4
Published
Node JS Stack Fast Configuration
Downloads
19
Maintainers
Readme
ENVIRONMENT REQUIREMENT
| NODE VERSION | NPM VERSION | |--------------|-------------| | 14.19.3 | 6.x.x |
PROJECT STRUCTURE APPLICATION
/ stackproject
└── public
└── assets
└── certs
└── repository
└── admin
└── ejs-app-1
└── ejs-app-2
└── clients
└── react-app-1
└── react-app-2
└── servers
└── controller
└── middleware
└── ModuleControl.js
└── model
└── connection
└── ModuleModel.js
└── routes
└── ApiRoutes.js
└── WebRoutes.js
COPY THE CODE BELOW TO GET TESTING INTO NPM RUN KIT (node 14.x.x) OR VISIT THIS LINK https://admhabits.github.io/snipkode-stack/
require("@google-cloud/firestore");
const Stack = require("@snipkode/stack");
const Routes = Stack.router();
const Jwt = Stack.jwt;
Routes.get('/', (req, res) => {
console.log(Jwt.sign('a', 'b')); // return eyJhbGciOiJIUzI1NiJ9.YQ.PrSVmEzsOLiTgzWfslar1L11W5nJXm8Vj0OFoGYUIjs
const token = "eyJhbGciOiJIUzI1NiJ9.YQ.PrSVmEzsOLiTgzWfslar1L11W5nJXm8Vj0OFoGYUIjs";
Jwt.verify(token, 'b', function(err, decoded) {
console.log(decoded) // return a
});
return res.status(Stack.status.OK).send({
message: 'hello world!'
});
});
const stackType = 'default';
const stackData = [
{
AppType: 'apis',
AppPort: 8234,
AppRoutes: Routes,
AppPath: false,
},
]
Stack.initializeApp({
type: stackType, secure: false,
servers: stackData
});
ALL STACK METHOD UTILITIES
| METHOD | RETURN TYPES | ARGUMENT | |------------------|--------------|-------------------------------------------------------------| | initializeApp | void | Object config | | createConnection | void | Object connection | | router | void | String route, callback(req, res) | | removeFile | void | String filePath, String fileName | | renameFile | void | String oldPath, String newPath, String fileName | | upload | void | Object { dest: './storages/' } | | arrayToObject | object | Arrays | | isEmpty | boolean | Object request body | | getDirectory | String | String path |
SETUP INITIALIZE APPLICATION
import Stack from '@snipkode/stack';
import ApiRoutes from 'path/to/routes/ApiRoutes';
import WebRoutes from 'path/to/routes/WebRoutes';
const stackType = process.env.NODE_ENV == 'production' ? 'extends' : 'default';
const stackData = [
{
AppType: 'apis',
AppPort: 8234,
AppRoutes: ApiRoutes,
AppPath: false,
},
{
AppType: 'admin',
AppPort: 9443,
AppRoutes: WebRoutes,
AppPath: '/admin',
},
{
AppType: 'client',
AppPort: 8000,
AppRoutes: false,
AppPath: '/clients',
}
]
Stack.initializeApp({
type: stackType, secure: true,
servers: stackData
});
CREATE CONNECTION MODULE
import Stack from "@snipkode/stack";
const dataConnection = {
admin: {
name: 'stackadmin',
user: 'root',
pass: '',
options: {
host: 'localhost',
dialect: 'mysql',
timezone: '+07:00'
}
},
client: {
name: 'stackclient',
user: 'root',
pass: '',
options: {
host: 'localhost',
dialect: 'mysql',
timezone: '+07:00'
}
}
}
const connect = Stack.createConnection(dataConnection);
export default connect;
CREATE MODEL PATTERN MODULE
import connect from 'path/to/connection';
import Stack from "@snipkode/stack";
export const Users = connect.admin.define('users', {
email: {
type: Stack.schema.STRING()
},
password: {
type: Stack.schema.CHAR(12)
}
})
CREATE CONTROLLER CLASS MODULE
import { Users } from "path/to/UserModel";
export default class UsersController {
static getUsers = async (req, res) => {
try {
const getUsers = await Users.findAll();
if(!getUsers){
res.status(404).send({
message: 'Tidak ditemukan data!'
})
}
res.status(200).send({
message: 'Data ditemukan!',
data: getUsers
})
} catch (error) {
res.status(500).send({
message: error.message
})
}
}
}
CREATE ROUTING STACK MODULE
import Stack from '@snipkode/stack';
import UsersController from 'path/to/controller/UsersController';
const ApiRouter = Stack.AppRouter();
ApiRouter.get('/', UsersController.getUsers);
ApiRouter.get('/users/:id', UsersController.updateUsers);
export default ApiRouter;
import { router } from '@snipkode/stack';
import UsersController from 'path/to/controller/UsersController';
const ApiRouter = router();
ApiRouter.get('/', UsersController.getUsers);
ApiRouter.get('/users/:id', UsersController.updateUsers);
export default ApiRouter;
FILE SYSTEM METHOD
const ApiRouter = router();
ApiRouter.get('/', upload({ dest: './storages/books/'}).single('thumb'), (req, res) => {
console.log(req.file);
const new_path_file = `${req.file.destination}/${req.file.filename}.${req.file.mimetype.split('/')[1]}`;
const path_file_removed = `${req.file.destination}/a36094022797a4a1512133fcc1d57173.jpeg`;
renameFile(req.file.path, new_file_path);
removeFile(path_file_removed);
res.send({
message: 'hi'
})
});
HANDLE STATUS CODE RESPONSE
import Stack, { status } from '@snipkode/stack';
Stack.AppRouter.get('/', (req, res) => {
return res.status(status.OK).send({
message: 'hi'
});
});
STATUS CODE REFERENCE
CONTINUE: "100",
SWITCHING_PROTOCOLS: "101",
PROCESSING: "102",
OK: "200",
CREATED: "201",
ACCEPTED: "202",
NON_AUTHORITATIVE_INFORMATION: "203",
NO_CONTENT: "204",
RESET_CONTENT: "205",
PARTIAL_CONTENT: "206",
MULTI_STATUS: "207",
MULTIPLE_CHOICES: "300",
MOVED_PERMANENTLY: "301",
MOVED_TEMPORARILY: "302",
SEE_OTHER: "303",
NOT_MODIFIED: "304",
USE_PROXY: "305",
TEMPORARY_REDIRECT: "307",
BAD_REQUEST: "400",
UNAUTHORIZED: "401",
PAYMENT_REQUIRED: "402",
FORBIDDEN: "403",
NOT_FOUND: "404",
METHOD_NOT_ALLOWED: "405",
NOT_ACCEPTABLE: "406",
PROXY_AUTHENTICATION_REQUIRED: "407",
REQUEST_TIME_OUT: "408",
CONFLICT: "409",
GONE: "410",
LENGTH_REQUIRED: "411",
PRECONDITION_FAILED: "412",
REQUEST_ENTITY_TOO_LARGE: "413",
REQUEST_URI_TOO_LARGE: "414",
UNSUPPORTED_MEDIA_TYPE: "415",
REQUESTED_RANGE_NOT_SATISFIABLE: "416",
EXPECTATION_FAILED: "417",
UNPROCESSABLE_ENTITY: "422",
LOCKED: "423",
FAILED_DEPENDENCY: "424",
UNORDERED_COLLECTION: "425",
UPGRADE_REQUIRED: "426",
PRECONDITION_REQUIRED: "428",
TOO_MANY_REQUESTS: "429",
REQUEST_HEADER_FIELDS_TOO_LARGE: "431",
INTERNAL_SERVER_ERROR: "500",
NOT_IMPLEMENTED: "501",
BAD_GATEWAY: "502",
SERVICE_UNAVAILABLE: "503",
GATEWAY_TIME_OUT: "504",
STATUS_VERSION_NOT_SUPPORTED: "505",
VARIANT_ALSO_NEGOTIATES: "506",
INSUFFICIENT_STORAGE: "507",
BANDWIDTH_LIMIT_EXCEEDED: "509",
NOT_EXTENDED: "510",
NETWORK_AUTHENTICATION_REQUIRED: "511"