JSFiddle - React, Tailwind, and code Playground

by velo_ninja

JavaScript

async function fetchFileContent(filePath) {
  const apiUrl = `http://localhost:5000/file?path=${encodeURIComponent(filePath)}`;
  try {
    const response = await fetch(apiUrl);
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const content = await response.text(); console.log(content);
    return content;
  } catch (error) {
    console.error(`Error: ${error.message}`);
    return null;
  }
}

async function handleFilePreview(filePath) {
  const content = await fetchFileContent(filePath);
  if (content !== null) {
    document.getElementById('previewArea').textContent = content;
  } else {
    alert('Error fetching file content.');
  }
}