Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous operations are not available in the http server #97

Open
gudaoxuri opened this issue Mar 22, 2023 · 0 comments
Open

Asynchronous operations are not available in the http server #97

gudaoxuri opened this issue Mar 22, 2023 · 0 comments

Comments

@gudaoxuri
Copy link

import { createServer, fetch } from 'http';

createServer((req, resp) => {
  req.on('data',async (body) => {

    if(req.url == '/wait'){
      print("fetch...");
      let response = await fetch('http://127.0.0.1:8001/echo', { method: 'POST', body: 'hello server' })
      let result = await response.text()
      print("fetched:",result);
      resp.end(result)
    }else{
      resp.end(body)
    }
    
  })
}).listen(8001,'0.0.0.0', () => {
  print('listen 8001 ...\n');
})

async function test_fetch() {
  let resp = await fetch('http://127.0.0.1:8001/wait', { method: 'POST', body: 'hello server' })
  print('fetch client recv:', await resp.text())
  print()
}

async function run_test() {
  await test_fetch()
  exit(0)
}

run_test()

Output after execution.

wait...
fetched: hello server

No output fetch client recv:hello server

So, after adding async/await operations to the http server, the request cannot get a response.

To return an empty response using CURL.

curl -d 'hello server' http://127.0.0.1:8001/wait -X POST
curl: (52) Empty response from the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant