ElasticAPM not working after xpack security enable

Hi after i enabled security fetaure in Elastic APM stooped working ,
Client Error:-
APM Server transport error (502): Unexpected APM Server response

elasticsearch.yml
   xpack.security.audit.enabled : true
  xpack.security.enabled : true
xpack.license.self_generated.type : basic```

Kibana.yml :
    xpack.security.enabled: true
   elasticsearch.username: elastic
   elasticsearch.password: pass

apm-server.yml
   output.elasticsearch:
           hosts: [ "0.0.0.0:9200" ]
           username : "elastic"
            password: "pass"

elastic agent script
  var apm = require('elastic-apm-node').start({
    serviceName: 'name',
    serverUrl: 'url'
  }); 

  var connect = require('connect')


// Add the Elastic APM middleware after your regular middleware
    app.use(apm.middleware.connect())

// ...but before any other error handler
    app.use(function (err, req, res, next) {
  // Custom error handling goes here
}); 

Hi,
sorry for the late reply. From the configuration you posted I don't see any changes in the APM agent -> server communication; do you see anything suspicious in the APM Server logs?