Created
May 29, 2019 13:16
-
-
Save yaasita/a0bcf6d1329c525b9d5cb584ed125519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const express = require("express"); | |
const util = require("util"); | |
const exec = util.promisify(require("child_process").exec); | |
const app = express(); | |
app.get("/", async (req, res) => { | |
const wait_time = parseInt(req.query.wait) || 150; | |
let state = false; | |
cmd(wait_time, () => { | |
state = true; | |
}); | |
{ | |
let i=0; | |
while(true){ | |
i++; | |
await sleep(1); | |
res.write(`wait ${i} seconds\n`); | |
if (state){ | |
break; | |
} | |
} | |
} | |
res.end(); | |
}); | |
async function cmd(wait_time, callback){ | |
console.log(`wait_time: ${wait_time}`); | |
const { stdout, stderr } = await exec(`sleep ${wait_time};echo ok`); | |
console.log(`stdout: ${stdout}`); | |
console.log(`stderr: ${stderr}`); | |
callback(); | |
} | |
function sleep(time){ | |
return new Promise((resolv,reject) => { | |
setTimeout(() => { | |
resolv(); | |
}, time * 1000); | |
}); | |
} | |
const port = process.env.PORT || 8080; | |
app.listen(port, () => { | |
console.log("listening on port", port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment