You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.2 KiB
89 lines
2.2 KiB
import http from 'http';
|
|
import * as url from "url";
|
|
import fetch from 'node-fetch';
|
|
|
|
|
|
const urlApi = `https://api.spacexdata.com/v3/launches/60`;
|
|
|
|
let dataApi = "";
|
|
|
|
fetch(urlApi)
|
|
.then((response) => response.json())
|
|
.then(function(data){
|
|
// Récupération des données api
|
|
dataApi = data;
|
|
}).catch(function(error){
|
|
console.log(error);
|
|
});
|
|
|
|
|
|
// Renvoie à l'adresse http://localhost:3000/
|
|
const server = http.createServer( // création du serveur
|
|
(request, response) => {
|
|
// création et envoi de la réponse
|
|
response.writeHead(200, {"Content-Type": "text/html"});
|
|
response.write(JSON.stringify(dataApi));
|
|
response.end();
|
|
}
|
|
);
|
|
|
|
server.listen(3000);
|
|
|
|
|
|
//Ancien code
|
|
// Création de l'entête de l'api
|
|
// function json_response(code, object, response) {
|
|
// response.statusCode = 200;
|
|
// response.setHeader('content-type', 'Application/json');
|
|
// response.end(JSON.stringify(object));
|
|
// }
|
|
|
|
// let data = "";
|
|
// const server = http.createServer(function(request, response) {
|
|
// let parseUrl = url.parse(request.url, true);
|
|
|
|
// if(request.method === "GET") {
|
|
// console.log('GET');
|
|
// data = getDragons(`https://api.spacexdata.com/v3/launches/60`);
|
|
// // Renvoyer la réponse JSON sur le navigateur
|
|
// console.log(data);
|
|
// }
|
|
// });
|
|
// server.listen(3000);
|
|
|
|
|
|
// // Avec un objet simple
|
|
// server.on('request', (request, response) => {
|
|
// const { method, url} = request;
|
|
// const content_type = 'text/html; charset=utf-8';
|
|
// response.setHeader('Content-Type', content_type); // (1)
|
|
// response.write(JSON.stringify(data))
|
|
// })
|
|
|
|
// function getDragons(url){
|
|
// let result = null;
|
|
// let promise = new Promise((resolve, reject) => {
|
|
// console.log(url);
|
|
// fetch(url).then(response => {
|
|
// if (response.ok) {
|
|
// response.json().then(data => resolve(data));
|
|
// } else {
|
|
// response.json().then(data => reject(data));
|
|
// }
|
|
// });
|
|
// });
|
|
|
|
// promise.then((data) => {
|
|
// result = data;
|
|
// console.log(data);
|
|
// }, (err) => {
|
|
// if(!err.canceled) {
|
|
// console.log(err);
|
|
// }
|
|
// })
|
|
|
|
// return result;
|
|
// }
|
|
|
|
|