$params = \Drupal\Core\Url::fromUserInput($path)->getRouteParameters();
if (isset($params['node'])) {
$node = \Drupal\node\Entity\Node::load($params['node']);
}
// or
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
// You can get nid and anything else you need from the node object.
$nid = $node->id();
}