Guest User

PHP server

a guest
Feb 4th, 2020
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http=require("http"),fs=require("fs"),exec=require("child_process").exec;
  2. var server=http.createServer((req,res)=> {
  3.     console.log(req.method,req.url);
  4.     if(req.url=="/") req.url="/index.php";
  5.     if(req.method=="GET") {
  6.         if(req.url.match(/\/.+\.php.*/)&&fs.existsSync("scripts/"+req.url)) {
  7.             console.log("OK",fs.readdirSync("scripts"));
  8.             exec("./php.exe scripts"+req.url, (error,stdout,stderr) => {
  9.                 if(error) console.log("error:",error);
  10.                 console.log("sending "+req.url+" to user");
  11.                 res.writeHead(200,{"Content-Type":"text/html"});
  12.                 res.end(stdout);
  13.             });
  14.         } else {
  15.             console.log("script",req.url,"not found");
  16.             res.writeHead(303,{"Location":"/"}); //303 redirection
  17.             res.end();
  18.         }
  19.     } else if(req.method=="POST") {
  20.         //POST handling impossible :(
  21.         var data="";
  22.         req.on("data",(chunk)=>data+=chunk);
  23.         req.on("end",()=>{
  24.             console.log("got data",data);
  25.             fs.writeFileSync("php_latestData.tmp",data);
  26.             res.writeHead(303,{"Location":req.url});
  27.             res.end();
  28.         });
  29.     } else if(req.method=="OPTIONS") {
  30.         //enable CORS
  31.         res.writeHead(200,
  32.         {"Access-Control-Allow-Methods":"GET,OPTIONS",
  33.         "Access-Control-Allow-Origin":"*"});
  34.         res.end();
  35.     } else {
  36.         res.writeHead(400);
  37.         res.end();
  38.     }
  39. }).listen(719); //uses  PHP 7.1.9
RAW Paste Data