Created
November 5, 2018 04:54
-
-
Save tanduong/c49919c93fffea6cf94e9cde05dd9b8a to your computer and use it in GitHub Desktop.
proxy and edit graphql response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const axios = require('axios'); | |
const app = express(); | |
const cors = require('cors'); | |
app.use(cors()); | |
app.use(bodyParser.json()); | |
const backend = 'https://marketplace-api.qa.kamereo.vn/graphql'; | |
app.post('/graphql', function(req, res) { | |
axios | |
.post(backend, req.body) | |
.then(function(response) { | |
try { | |
const {data, errors, dataPresent, extensions} = response.data; | |
res.json({ | |
data, | |
errors: errors.length > 0 ? errors : null, | |
dataPresent, | |
extensions, | |
}); | |
} catch (error) { | |
console.error(error); | |
res.json({}); | |
} | |
}) | |
.catch(function(error) { | |
console.log(error); | |
}); | |
}); | |
const server = require('http').createServer(app); | |
server.listen(3333); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment