code: def _start_rest_server(
      nni_manager_args: NniManagerArgs, run_mode: RunMode
  ) -> Popen:
      import nni_node
  
      node_dir = Path(nni_node.__path__[0])  # type: ignore
      node = str(
          node_dir / ("node.exe" if sys.platform == "win32" else "node")
      )
      main_js = str(node_dir / "main.js")
      cmd = [
          node,
          "--max-old-space-size=4096",
          "--trace-uncaught",
          main_js,
      ]
      cmd += nni_manager_args.to_command_line_args()
  
      if run_mode.value == "detach":
          log = Path(
              nni_manager_args.experiments_directory,
              nni_manager_args.experiment_id,
              "log",
          )
          out = (log / "nnictl_stdout.log").open("a")
          err = (log / "nnictl_stderr.log").open("a")
          header = f"Experiment {nni_manager_args.experiment_id} start: {datetime.now()}"
          header = "-" * 80 + "\n" + header + "\n" + "-" * 80 + "\n"
          out.write(header)
          err.write(header)
  
      else:
          out = None
          err = None
  
      if sys.platform == "win32":
          from subprocess import CREATE_NEW_PROCESS_GROUP
  
          return Popen(
              cmd,
              stdout=out,
              stderr=err,
              cwd=node_dir,
              creationflags=CREATE_NEW_PROCESS_GROUP,
          )
      else:
          return Popen(
              cmd,
              stdout=out,
              stderr=err,
              cwd=node_dir,
              preexec_fn=os.setpgrp,
          )
  
imports: from __future__ import annotations
  import contextlib
  from dataclasses import dataclass, fields
  from datetime import datetime
  import logging
  import os.path
  from pathlib import Path
  import socket
  from subprocess import Popen
  import sys
  import time
  from typing import Any, TYPE_CHECKING, cast
  from typing_extensions import Literal
  from .config import ExperimentConfig
  from .None import rest
  from ..tools.nnictl.config_utils import Experiments, Config
  from ..tools.nnictl.nnictl_utils import update_experiment
label_desc: Use image node_id 2 for calling a global function with example usage: _start_rest_server(nni_manager_args, run_mode) and returns: Popen, Popen