Created
September 7, 2018 10:29
-
-
Save Denkong/00338859b423c80c413bf0aa72ad8597 to your computer and use it in GitHub Desktop.
Node/Express - Пример отправки HTTP запроса через backend и создание api
This file contains 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
///При работе с JS и отправки запроса на api чужих серверов, выйдет ошибка, так как должны присутсвовать заголовки CORS | |
///Можно создать api на своем backend, и обращаться уже к нему | |
var express = require('express'); | |
var app = express(); | |
var request = require('request'); | |
app.get('/', function (req, res) { | |
request('http://auction-api-eu.worldofwarcraft.com/auction-data/.../auctions.json', function (error, response, body) { | |
console.log('error:', error); // Print the error if one occurred | |
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | |
data = JSON.parse(body); | |
res.send('Запрос выполнился успешно ' + JSON.stringify(data.realms[0].name)); | |
}); | |
}); | |
app.get('/api/GetRealmName', (req, res) => { | |
request('http://auction-api-eu.worldofwarcraft.com/auction-data/.../auctions.json', function (error, response, body) { | |
console.log('error:', error); | |
console.log('statusCode:', response && response.statusCode); | |
data = JSON.parse(body); | |
res.json(data.realms) | |
}); | |
}) | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment