Fictizia / Master-en-programacion-de-aplicaciones-con-JavaScript-y-Node.js_ed1

FICTIZIA » Master en programación de aplicaciones con JavaScript y Node.js — 1ª Edición
http://fictizia.com/formacion/master-javascript-nodejs
GNU Affero General Public License v3.0
38 stars 23 forks source link

Express super Hack: Proxy con Cors + Estáticos + Root #66

Closed UlisesGascon closed 6 years ago

UlisesGascon commented 6 years ago

Version avanzada de #56

// npm install express request
var express = require('express');
var request = require('request');
var app = express();
var path = require('path');

// Static files
app.use(express.static('public'));

// viewed at http://localhost:8080
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});

app.get('/proxy', function (req, res){
    // @Example: localhost:8080/proxy?url=http://airemad.com/api/v1/station/

    // CORS
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    // PIPE Response
    request({  
        url: req.query.url,
        method: req.query.method || "GET"
    }).pipe(res)
});

app.listen(8080);