upload files into server
This commit is contained in:
parent
679a661c4f
commit
18f37056cd
@ -1,7 +1,7 @@
|
|||||||
const backend = require('./index');
|
const njsbacker = require('./index');
|
||||||
|
|
||||||
// Создаём класс бэкенда, наследующий класс backend.Main
|
// Создаём класс бэкенда, наследующий класс njsbacker.Main
|
||||||
class Main extends backend.Main {
|
class Main extends njsbacker.Main {
|
||||||
/* errorHandler (error) { return {
|
/* errorHandler (error) { return {
|
||||||
mainbody : JSON.stringify({ error : {}, error }),
|
mainbody : JSON.stringify({ error : {}, error }),
|
||||||
headers : {
|
headers : {
|
||||||
@ -19,7 +19,7 @@ class Main extends backend.Main {
|
|||||||
console.log(sessionData.example); // Получить значение из сессии
|
console.log(sessionData.example); // Получить значение из сессии
|
||||||
sessionData._remove('example'); // Убрать значение
|
sessionData._remove('example'); // Убрать значение
|
||||||
return 1; // Успешно
|
return 1; // Успешно
|
||||||
return 'Example error' // Пример ошибки
|
throw 'Example error' // Пример ошибки
|
||||||
}
|
}
|
||||||
|
|
||||||
responseHandler (response) { return ({
|
responseHandler (response) { return ({
|
||||||
@ -42,14 +42,13 @@ server.setSessionParams( // Зададим необходимые параме
|
|||||||
{
|
{
|
||||||
session_id : {
|
session_id : {
|
||||||
required : false,
|
required : false,
|
||||||
type : backend.types.integer
|
type : njsbacker.types.integer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
server.typeError = 'param {param} must be only {long_type} ({short_type})';
|
|
||||||
|
|
||||||
// Создаём класс группы методов
|
// Создаём класс группы методов
|
||||||
class ExampleMethodGroup extends backend.Group {
|
class ExampleMethodGroup extends njsbacker.Group {
|
||||||
handler (params, session) { // Путевая обработка
|
handler (params, session) { // Путевая обработка
|
||||||
session._setValue('example', 1); // Задать значение
|
session._setValue('example', 1); // Задать значение
|
||||||
console.log(session.example); // Получить значение из сессии
|
console.log(session.example); // Получить значение из сессии
|
||||||
@ -59,7 +58,7 @@ class ExampleMethodGroup extends backend.Group {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Создаём классы методов
|
// Создаём классы методов
|
||||||
class ExampleAnyMethodsOfHandlingInformation extends backend.Method {
|
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||||
execute (params, session, groups) {
|
execute (params, session, groups) {
|
||||||
return {
|
return {
|
||||||
json_data : params.json_name,
|
json_data : params.json_name,
|
||||||
@ -69,7 +68,7 @@ class ExampleAnyMethodsOfHandlingInformation extends backend.Method {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ExampleMethod extends backend.Method {
|
class ExampleMethod extends njsbacker.Method {
|
||||||
/*
|
/*
|
||||||
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||||
*/
|
*/
|
||||||
@ -84,18 +83,18 @@ class ExampleMethod extends backend.Method {
|
|||||||
session_id : params.session_id
|
session_id : params.session_id
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
throw new backend.ApiError('EXAMPLE_ERROR', new Object());
|
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SumMethod extends backend.Method {
|
class SumMethod extends njsbacker.Method {
|
||||||
execute (params, session, groups) {
|
execute (params, session, groups) {
|
||||||
return params.a + params.b;
|
return params.a + params.b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FileMethod extends backend.Method {
|
class FileMethod extends njsbacker.Method {
|
||||||
execute (params, session, groups) {
|
execute (params, session, groups) {
|
||||||
return JSON.stringify(params.file);
|
return JSON.stringify(params.file);
|
||||||
}
|
}
|
||||||
@ -105,13 +104,13 @@ class FileMethod extends backend.Method {
|
|||||||
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||||
queryName : {
|
queryName : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.string,
|
type : njsbacker.types.string,
|
||||||
import_key : 'name',
|
import_key : 'name',
|
||||||
allow_params : ['query']
|
allow_params : ['query']
|
||||||
},
|
},
|
||||||
jsonName : {
|
jsonName : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.string,
|
type : njsbacker.types.string,
|
||||||
import_key : 'name',
|
import_key : 'name',
|
||||||
allow_methods : ['post'],
|
allow_methods : ['post'],
|
||||||
allow_params : ['json']
|
allow_params : ['json']
|
||||||
@ -121,20 +120,20 @@ var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
|||||||
var fileMethod = new FileMethod('file', '/file', {
|
var fileMethod = new FileMethod('file', '/file', {
|
||||||
file : {
|
file : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.file()
|
type : njsbacker.types.file()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var sumMethod = new SumMethod('sum', '/sum', {
|
var sumMethod = new SumMethod('sum', '/sum', {
|
||||||
a : {
|
a : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.integer,
|
type : njsbacker.types.integer,
|
||||||
conversion : false,
|
conversion : false,
|
||||||
// allow_methods : ['post'],
|
// allow_methods : ['post'],
|
||||||
},
|
},
|
||||||
b : {
|
b : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.integer,
|
type : njsbacker.types.integer,
|
||||||
conversion : false,
|
conversion : false,
|
||||||
// allow_methods : ['post'],
|
// allow_methods : ['post'],
|
||||||
}
|
}
|
||||||
@ -143,7 +142,7 @@ var sumMethod = new SumMethod('sum', '/sum', {
|
|||||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||||
text : {
|
text : {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.string,
|
type : njsbacker.types.string,
|
||||||
conversion : false,
|
conversion : false,
|
||||||
values : ['123', 'test'],
|
values : ['123', 'test'],
|
||||||
min_length : 1,
|
min_length : 1,
|
||||||
@ -155,12 +154,12 @@ var exampleMethod = new ExampleMethod('example', '/example', {
|
|||||||
// Привяжем метод к группе
|
// Привяжем метод к группе
|
||||||
exampleMethod.group(new ExampleMethodGroup({
|
exampleMethod.group(new ExampleMethodGroup({
|
||||||
ses : {
|
ses : {
|
||||||
type : backend.types.string
|
type : njsbacker.types.string
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
sumMethod.group(new ExampleMethodGroup({
|
sumMethod.group(new ExampleMethodGroup({
|
||||||
ses : {
|
ses : {
|
||||||
type : backend.types.string
|
type : njsbacker.types.string
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
// Привяжем метод к основному проекту
|
// Привяжем метод к основному проекту
|
||||||
|
@ -62,11 +62,62 @@ const types = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
bool : (trueValue = '1') => {
|
||||||
|
long_name : 'boolean',
|
||||||
|
short_name : 'bool',
|
||||||
|
checkSchema : (value, schema) => {
|
||||||
|
if (schema.min_length != undefined) { // min_length
|
||||||
|
if (value.length < schema.min_length) {
|
||||||
|
return [false, 'minLengthError'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schema.max_length != undefined) { // max_length
|
||||||
|
if (value.length > schema.max_length) {
|
||||||
|
return [false, 'maxLengthError'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [true, 'ok'];
|
||||||
|
},
|
||||||
|
syntax : (value, needs_convert = false) => {
|
||||||
|
if (needs_convert) {
|
||||||
|
return [true, value === trueValue]
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return [typeof(value) == 'boolean', typeof(value) == 'boolean' ? value : undefined]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
array : (splitter, type=types.dynamic) => ({
|
array : (splitter, type=types.dynamic) => ({
|
||||||
long_name : `array (${type.long_name})`,
|
long_name : `array (${type.long_name})`,
|
||||||
short_name : `arr (${type.short_name})`,
|
short_name : `arr (${type.short_name})`,
|
||||||
checkSchema : (value, schema) => {},
|
checkSchema : (value, schema) => {
|
||||||
syntax : (value, needs_convert = false) => {}
|
if (schema.min_length != undefined) { // min_length
|
||||||
|
if (value.length < schema.min_length) {
|
||||||
|
return [false, 'minLengthError'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schema.max_length != undefined) { // max_length
|
||||||
|
if (value.length > schema.max_length) {
|
||||||
|
return [false, 'maxLengthError'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [true, 'ok'];
|
||||||
|
},
|
||||||
|
syntax : (value, needs_convert = false) => {
|
||||||
|
if (typeof(value) == 'string' & !!needs_convert) {
|
||||||
|
value = value.split(splitter);
|
||||||
|
}
|
||||||
|
else if (typeof(value) != 'object') {
|
||||||
|
return [false, undefined];
|
||||||
|
}
|
||||||
|
// checking type of array
|
||||||
|
let isSyntax = Object.assign({}, value).filter(item => type.syntax(item, needs_convert)[0]).length == value.length;
|
||||||
|
return [isSyntax, isSyntax ? value : undefined]
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
integer : {
|
integer : {
|
||||||
long_name : 'integer',
|
long_name : 'integer',
|
||||||
|
226
readme.md
226
readme.md
@ -134,7 +134,229 @@ var app = new App(
|
|||||||
false // Returns information about njsbacker in headers.
|
false // Returns information about njsbacker in headers.
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
If you're planning use session you can configure him:
|
||||||
|
```javascript
|
||||||
|
app.setSessionParams(
|
||||||
|
{
|
||||||
|
example_session_param : {
|
||||||
|
required : false,
|
||||||
|
type : njsbacker.types.string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
**First param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||||
### Method
|
### Method
|
||||||
|
After you create **njsbacker.Main** object you must create class that extends from **njsbacker.Method**:
|
||||||
|
```javascript
|
||||||
|
class ExampleMethod extends njsbacker.Method {
|
||||||
|
// Params handler
|
||||||
|
execute (params, session, groups) {
|
||||||
|
// Any code...
|
||||||
|
return anotherResult;
|
||||||
|
// If you needs raising error:
|
||||||
|
throw new njsbacker.ApiError(
|
||||||
|
'EXAMPLE_ERROR', // Error code
|
||||||
|
new Object() // Error details
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
#### Method: execute
|
||||||
|
Method of executing current method. Return data when will be sended to njsbacker.Main.responseHandler method.
|
||||||
|
#### Connection and configure njsbacker.Method
|
||||||
|
After creating class you must create object of njsbacker.Method:
|
||||||
|
```javascript
|
||||||
|
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||||
|
text : {
|
||||||
|
required : true,
|
||||||
|
type : backend.types.string,
|
||||||
|
min_length : 1,
|
||||||
|
max_length : 255
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
**First param** - name of method into system.
|
||||||
|
**Second param** - path to method into http-server
|
||||||
|
**Third param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||||
|
If you create object of **njsbacker.Group** you can pin it with use next method:
|
||||||
|
```javascript
|
||||||
|
exampleMethod.group(groupObject);
|
||||||
|
```
|
||||||
|
And that this method work you must pin this method to object of **njsbacker.Main** with use next method:
|
||||||
|
```javascript
|
||||||
|
app.method(exampleMethod);
|
||||||
|
```
|
||||||
|
#### Additional tool into njsbacker.Method: this.MainObject.call
|
||||||
## Additional clases and objects
|
## Additional clases and objects
|
||||||
## Examples
|
### Params Information
|
||||||
|
Before reading this part we tolds about inputed params into http.
|
||||||
|
## Example code
|
||||||
|
```javascript
|
||||||
|
const njsbacker = require('./index');
|
||||||
|
|
||||||
|
// Create backend mainclass then extends from njsbacker.Main:
|
||||||
|
class Main extends njsbacker.Main {
|
||||||
|
session (params, sessionData) {
|
||||||
|
sessionData._setValue('example', 1); // Set value
|
||||||
|
console.log(sessionData.example); // Get value from session
|
||||||
|
sessionData._remove('example'); // Remove value
|
||||||
|
return 1; // Successful
|
||||||
|
throw 'Example error' // Example of error
|
||||||
|
}
|
||||||
|
|
||||||
|
responseHandler (response) { return ({
|
||||||
|
mainbody : { response },
|
||||||
|
headers : {
|
||||||
|
errored: 0
|
||||||
|
},
|
||||||
|
cookies : {},
|
||||||
|
// redirect_uri: ''; // if want redirect to another url
|
||||||
|
code: 200
|
||||||
|
}) };
|
||||||
|
|
||||||
|
/* paramsError (required, additional) { return({ required, additional }) }; */
|
||||||
|
}
|
||||||
|
// Create object of Main class.
|
||||||
|
var server = new Main(
|
||||||
|
false // Show information about this library into headers.
|
||||||
|
);
|
||||||
|
server.setSessionParams( // Set required params for session.
|
||||||
|
{
|
||||||
|
session_id : {
|
||||||
|
required : false,
|
||||||
|
type : njsbacker.types.integer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create class from method's group.
|
||||||
|
class ExampleMethodGroup extends njsbacker.Group {
|
||||||
|
handler (params, session) { // Path handling
|
||||||
|
session._setValue('example', 1); // Set value
|
||||||
|
console.log(session.example); // Get value from session
|
||||||
|
session._remove('example'); // Remove value
|
||||||
|
return 1; // Successful
|
||||||
|
throw 'Example error' // Example of error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Create classes of method
|
||||||
|
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||||
|
execute (params, session, groups) {
|
||||||
|
return {
|
||||||
|
json_data : params.json_name,
|
||||||
|
query_data : params.query_name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleMethod extends njsbacker.Method {
|
||||||
|
/*
|
||||||
|
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Params handler
|
||||||
|
execute (params, session, groups) {
|
||||||
|
return {
|
||||||
|
text : params.text,
|
||||||
|
result : this.MainObject.call('sum', {
|
||||||
|
a : 15,
|
||||||
|
b : 17,
|
||||||
|
session_id : params.session_id
|
||||||
|
})
|
||||||
|
};
|
||||||
|
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SumMethod extends njsbacker.Method {
|
||||||
|
execute (params, session, groups) {
|
||||||
|
return params.a + params.b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FileMethod extends njsbacker.Method {
|
||||||
|
execute (params, session, groups) {
|
||||||
|
return JSON.stringify(params.file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create class objects
|
||||||
|
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||||
|
queryName : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.string,
|
||||||
|
import_key : 'name',
|
||||||
|
allow_params : ['query']
|
||||||
|
},
|
||||||
|
jsonName : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.string,
|
||||||
|
import_key : 'name',
|
||||||
|
allow_methods : ['post'],
|
||||||
|
allow_params : ['json']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var fileMethod = new FileMethod('file', '/file', {
|
||||||
|
file : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.file()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var sumMethod = new SumMethod('sum', '/sum', {
|
||||||
|
a : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.integer,
|
||||||
|
conversion : false,
|
||||||
|
// allow_methods : ['post'],
|
||||||
|
},
|
||||||
|
b : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.integer,
|
||||||
|
conversion : false,
|
||||||
|
// allow_methods : ['post'],
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||||
|
text : {
|
||||||
|
required : true,
|
||||||
|
type : njsbacker.types.string,
|
||||||
|
conversion : false,
|
||||||
|
values : ['123', 'test'],
|
||||||
|
min_length : 1,
|
||||||
|
max_length : 255,
|
||||||
|
// allow_methods : ['post'],
|
||||||
|
// allow_params : ['json'],
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Pins methods to group
|
||||||
|
exampleMethod.group(new ExampleMethodGroup({
|
||||||
|
ses : {
|
||||||
|
type : njsbacker.types.string
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
sumMethod.group(new ExampleMethodGroup({
|
||||||
|
ses : {
|
||||||
|
type : njsbacker.types.string
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
// Pin methods to mein project
|
||||||
|
server.method(exampleMethod);
|
||||||
|
server.method(sumMethod);
|
||||||
|
server.method(fileMethod);
|
||||||
|
server.method(eamohi);
|
||||||
|
|
||||||
|
// Run server
|
||||||
|
server.server('/api/v1').listen(8080, async (err) => {
|
||||||
|
if (err) { throw err; }
|
||||||
|
else {
|
||||||
|
console.log('SERVER RUNNED');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
You can show example code in file **codeExample.js**
|
Loading…
Reference in New Issue
Block a user