diff --git a/codeExample.js b/codeExample.js index 354bd4d..af63078 100644 --- a/codeExample.js +++ b/codeExample.js @@ -50,8 +50,7 @@ server.setSessionParams( // Зададим необходимые параме { session_id : { required : true, - type : backend.types.integer, - values : [1] + type : backend.types.integer } } ); @@ -94,7 +93,20 @@ class SumMethod extends backend.Method { } } +class FileMethod extends backend.Method { + execute (params, session, groups) { + return JSON.stringify(params.file); + } +} + // Создаём экземпляры классов +var fileMethod = new FileMethod('file', '/file', { + file : { + required : true, + type : backend.types.file() + } +}); + var sumMethod = new SumMethod('sum', '/sum', { a : { required : true, @@ -136,6 +148,7 @@ sumMethod.group(new ExampleMethodGroup({ // Привяжем метод к основному проекту server.method(exampleMethod); server.method(sumMethod); +server.method(fileMethod); // Запускаем сервер server.server('/api/v1').listen(8080, async (err) => { diff --git a/components/Main.js b/components/Main.js index 62456dd..431696f 100644 --- a/components/Main.js +++ b/components/Main.js @@ -12,6 +12,9 @@ class ApiError extends Error { } +const debug = (text) => {} + + class Main { constructor (sendHeaders = true) { this.sendHeaders = sendHeaders; @@ -79,15 +82,19 @@ class Main { this.fileUploadConfig = fileUploadConfig; } - router (returnMiddlewareFunction = false, middlewareFunction = (req, res, next) => next(), debug = (text) => console.log(text)) { + router (returnMiddlewareFunction = false, middlewareFunction = (req, res, next) => next()) { let router = express.Router(); router.use(require('cookie-parser')()); // parse various different custom JSON types as JSON router.use(bodyParser.json({ type: 'application/*+json' })) + router.use(bodyParser.json({ type: 'application/json' })) // parse some custom thing into a Buffer router.use(bodyParser.raw({ type: 'application/vnd.custom-type' })) // parse an HTML body into a string router.use(bodyParser.text({ type: 'text/html' })); + // parse application/x-www-form-urlencoded + router.use(bodyParser.urlencoded({ type: 'application/x-www-form-urlencoded', extended: false })) + // parse files router.use(require('express-fileupload')(this.fileUploadConfig)); diff --git a/components/Method.js b/components/Method.js index 15fca0a..5aa0d9f 100644 --- a/components/Method.js +++ b/components/Method.js @@ -132,15 +132,15 @@ class Method { case 'headers' : [isSyntax, convertedValue] = paramScheme.type.syntax(headers[param], true); break; + case 'body' : + [isSyntax, convertedValue] = paramScheme.type.syntax(body[param], true); + break; case 'json' : [isSyntax, convertedValue] = paramScheme.type.syntax(json[param], paramScheme.conversion); break; case 'params' : [isSyntax, convertedValue] = paramScheme.type.syntax(params[param], paramScheme.conversion); break; - case 'body' : - [isSyntax, convertedValue] = paramScheme.type.syntax(body[param], paramScheme.conversion); - break; case 'files' : [isSyntax, convertedValue] = paramScheme.type.syntax(files[param], paramScheme.conversion); break; diff --git a/package.json b/package.json index 8732df4..14668a3 100644 --- a/package.json +++ b/package.json @@ -4,5 +4,29 @@ "cookie-parser": "^1.4.6", "express": "^4.18.2", "express-fileupload": "^1.4.0" - } + }, + "name": "njsbacker", + "version": "0.9.0", + "description": "Module for easy developing back-end projects at express.js", + "main": "index.js", + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Nikiroy78/njsbacker.git" + }, + "keywords": [ + "backend", + "back-end", + "express", + "express.js" + ], + "author": "Nikiroy78", + "license": "ISC", + "bugs": { + "url": "https://github.com/Nikiroy78/njsbacker/issues" + }, + "homepage": "https://github.com/Nikiroy78/njsbacker#readme" }