Create node before page load I have a web service on third party system that return property of doctor and I have a content type named "doctor". 
I want when user go to a url like doctor/23 drupal create a node of type doctor based on url arg and show this node to user.
for that and with @zaporylie help i create a custom controller like bellow

create a Routing:

    showdoctor.load_doctor_controller_load:
  path: 'doctor/{doctor_id}'
  defaults:
    _controller: '\Drupal\showdoctor\Controller\LoadDoctorController::load'
    _title: 'load'
  requirements:
    _permission: 'access content'


and my controller like this:

    class LoadDoctorController extends ControllerBase
{
    public function load($doctor_id)
    {
        $result = RestCurl::post("http://myrest_server.com", [
            'filter' =&gt; ['doctor.id' =&gt; $doctor_id],
        ]);

        $fullname = $result['data']-&gt;message-&gt;data[0]-&gt;firstName;
        $fullname = $fullname . ' ' . $result['data']-&gt;message-&gt;data[0]-&gt;lastName;
        // Fetch doctor node with EFQ
        $query = \Drupal::entityQuery('node');
        $query-&gt;condition('type', 'doctor');
        $query-&gt;condition('field_doctor_id', $doctor_id);
        $nids = $query-&gt;execute();
        if (!$nids) {
            $node = Node::create([
                'type' =&gt; 'doctor',
                'title' =&gt; $fullname,
                'uid' =&gt; 1,
            ]);
            $node-&gt;set('field_doctor_id', $doctor_id);
            $node-&gt;save();
            $element = array(
                '#markup' =&gt; $node,
            );
            return $element;
        } else {
            $element = array(
                '#markup' =&gt; $node,
            );
            return $element;
        }
    }

}


Now for example when in go to doctor/23 page i see a page with load title. and if i refresh this page i see doctor page correctly. i think my problem is return value.
You should create a custom Controller rather than hook on page load. This way you won't have an overhead on every page load. In the Controller (accessible under movie/{movie_id} route) try to load movie by movie_id (entityQuery), and if it does not exists simply create one based on data from 3rd party service and display in a display mode of your choice.