Skip to content

Instantly share code, notes, and snippets.

@gabonator

gabonator/server.js

Created Apr 4, 2019
Embed
What would you like to do?
nodejs web server with cors workaround
const express = require('express');
const app = express();
const spawn = require('child_process').spawn;
const request = require('request');
app.post("/cors/*", function(req, res)
{
var url = decodeURIComponent(req.url.substr(6));
var body = '';
req.on("data",function(chunk){
body += chunk.toString();
});
req.on("end",function(){
var options = {uri: url, method: 'POST', json: JSON.parse(body)};
console.log(options);
request(options, function (error, response, body) {
if (!error && response.statusCode == 200)
{
var txt = JSON.stringify(body);
res.write(txt);
res.end();
}
});
});
});
app.get("/cors/*", function(req, res) {
console.log(req.url);
var url = decodeURIComponent(req.url.substr(6));
request(url, function(error, response, body)
{
if (!error && response.statusCode == 200) {
res.write(body);
res.end();
}
});
});
app.get("*", function(req, res) {
console.log(req.url);
if (req.url == "/")
res.sendFile(__dirname + "/index.html")
else
res.sendFile(__dirname + req.url)
});
app.listen(8082, function() {
console.log("Test service started at localhost:8082");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.