var fetch = require("node-fetch");
var http = require("http");
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });
let myGlobalData;
// Running Server Details.
var server = app.listen(8082, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at %s:%s Port", host, port)
});
app.get('/form', function (req, res) {
var html='';
html +="<body>";
html += "<form action='/thank' method='post' name='form1'>";
html += "web URL:</p><input type='text' name='weburl'>";
html += "<input type='submit' value='submit'>";
html += "<INPUT type='reset' value='reset'>";
html += "</form>";
html += "</body>";
res.send(html);
});
app.post('/thank', urlencodedParser, function (req, res){
let reply='';
let myPromise = fetch('https://facebook.github.io/react-native/movies.json');
myPromise.then(function(response) {
return response.text()
}).then(function(text) {
// var myLocalData = text;
myGlobalData = text;
myLocalData = text;
// console.log(myLocalData);
return text;
})
.then(function(data) {
// (4) Option #2: stay in this promise chain
console.log('Option #2: ' + data);
res.send(data);
return data;
});
// console.log(myGlobalData); // result is undefined Why?
// console.log(mydata);
myPromise.then(function(data) {
// this code can execute when 'myPromise' is resolved
console.log('Option #3: ' + data);
})
//reply += "Your URL submitted: " + myPromise.then(function(data) {
// console.log('Option #4' + data);
// res.send(reply);
// return data
// + myData; // req.body.weburl;
// res.send(reply);
// fetch('http://localhost:8080/' + req.body.weburl)
// .then(res => res.text())
// .then(body => console.log(body))
// .then(function(response) {
// // console.log(res.text());
// console.log(response);
// // reply += body;
// // res.send(body);
// // reply += data;
// })
// .catch(function(error) {
// console.log(error);
// });
var locales = {
europe: function() { // The Europe continent's local scope
var myFriend = "Monique";
var france = function() { // The France country's local scope
var paris = function() { // The Paris city's local scope
console.log(myFriend);
};
paris();
};
france();
}
};
locales.europe();
var test = "I'm global";
function testScope() {
var test = "I'm local";
console.log (test);
}
testScope(); // output: I'm local
console.log(test); // output: I'm global
var changeable;
function changeSomething(){ changeable = 'bla';}
changeSomething();
console.log(changeable);
// mydata.then(response => {
// if(response.ok){
// response.json().then((data) => {
// myData = data;
// console.log(myData);
// res.send(myData);
// // reply += data;
// // console.log(data)
// });
// }else{
// throw 'There is something wrong'
// }
// }).
// catch(error => {
// console.log(error);
// });
// console.log(myData);
});
Monique
I'm local
I'm global
bla
Option #3: [object Object]
Option #2: {
"title": "The Basics - Networking",
"description": "Your app fetched this from a remote endpoint!",
"movies": [
{ "title": "Star Wars", "releaseYear": "1977"},
{ "title": "Back to the Future", "releaseYear": "1985"},
{ "title": "The Matrix", "releaseYear": "1999"},
{ "title": "Inception", "releaseYear": "2010"},
{ "title": "Interstellar", "releaseYear": "2014"}
]
}
Правильный ответ был вариант № 2, который помог распечатать результаты данных, которые были получены из контракта myPromise.Результаты были отправлены с помощью res.send (data) из .then (function (data)) {
console.log ('Option # 2:' + data);
res.send (data);
возврат данных;}