From 1054fc529b787a09b9f2b5e0ae7e253484dc6163 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Wed, 6 Dec 2017 18:17:50 +0530 Subject: [PATCH] Fix : cors middleware - #12 --- bin/index.js | 2 ++ index.js | 2 ++ lib/xctrl.js | 2 +- lib/xsql.js | 2 +- package-lock.json | 18 +++++++++++++++++- package.json | 1 + 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bin/index.js b/bin/index.js index a9e881cded..4a50e8c536 100755 --- a/bin/index.js +++ b/bin/index.js @@ -5,6 +5,7 @@ const bodyParser = require('body-parser'); const express = require('express'); const sqlConfig = require('commander'); const mysql = require('mysql'); +const cors = require('cors'); const dataHelp = require('../lib/util/data.helper.js'); const Xapi = require('../lib/xapi.js'); const cmdargs = require('../lib/util/cmd.helper.js'); @@ -16,6 +17,7 @@ cmdargs.handle(sqlConfig) /**************** START : setup express ****************/ let app = express(); app.use(morgan('tiny')) +app.use(cors()) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true diff --git a/index.js b/index.js index 0b27c862f4..90c59a002d 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const bodyParser = require('body-parser'); const express = require('express'); const sqlConfig = require('commander'); const mysql = require('mysql'); +const cors = require('cors'); const dataHelp = require('./lib/util/data.helper.js'); const Xapi = require('./lib/xapi.js'); @@ -17,6 +18,7 @@ cmdargs.handle(sqlConfig) /**************** START : setup express ****************/ let app = express(); app.use(morgan('tiny')) +app.use(cors()) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true diff --git a/lib/xctrl.js b/lib/xctrl.js index 6b81ab741a..50f6120072 100644 --- a/lib/xctrl.js +++ b/lib/xctrl.js @@ -197,7 +197,7 @@ class xctrl { //console.log(req.app.locals._tableName, req.body); - this.mysql.getBulkInsertStatement(req.app.locals._tableName, req.body, queryParamsObj) + this.mysql.prepareBulkInsert(req.app.locals._tableName, req.body, queryParamsObj) results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); res.status(200).json(results); diff --git a/lib/xsql.js b/lib/xsql.js index 4f89a52baa..0386454541 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -206,7 +206,7 @@ class Xsql { } - getBulkInsertStatement(tableName, objectArray, queryParamsObj) { + prepareBulkInsert(tableName, objectArray, queryParamsObj) { if (tableName in this.metaDb.tables && objectArray) { diff --git a/package-lock.json b/package-lock.json index f35e9f435a..f85da4c548 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.1.9", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -194,6 +194,22 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cors": { + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.4.tgz", + "integrity": "sha1-K9OB8usgECAQXNUOpZ2mMJBpRoY=", + "requires": { + "object-assign": "4.1.1", + "vary": "1.1.2" + }, + "dependencies": { + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + } + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index 396a7cf78b..3f4f70907f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "body-parser": "^1.18.2", "colors": "^1.1.2", "commander": "^2.11.0", + "cors": "^2.8.4", "express": "^4.16.1", "mocha": "^4.0.1", "morgan": "^1.9.0",