Here is my code :
const koa = require('koa');
const app = new koa();
const https = require('https')
let email = "myemail@gmail.com"
let password = "mysecret"
var options = {
host: 'myhost',
port: 443,
path: '/api/login?email=' + email + '&password=' + password,
method: 'POST'
};
async function https_req(options, callback) {
https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
callback(chunk)
})
}).end();
}
app.use(async (ctx, next) => {
let res = await https_req(options, (result) => {
console.log("final result: ",result)
ctx.body = result
})
})
app.listen(3000)
In this code, it gives proper result in console but in webpage it only shows Not Found
I have tried this with express and it was working perfectly fine but then I wanted to give a shot to Koa. It is working properly if I just type ctx.body = 'some data'
without calling any function. I think koa is not waiting even if I write await.
I also tried async await inside callback function :
await https_req(options, async (result) => {
console.log("final result: ",result)
ctx.body = await result
})
But it always gives "Not Found".
I also want to know why this is happening. And what should I do to make it work.