Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
replace react .tsx file template content with actual data

wondering if it's possible to somehow replace a template file with actual content (coming from an api). E.g. the template looks like this:

import { NextPage } from "next";

const Page: NextPage = () => {
    return (
        <Layout>
            {content}
        </Layout>
    )
};

export default Page;

the following would have to be replaced: Page with the actual file name, Layout with the corresponding layout that file uses, and content with the page`s content.

Now I am able to create a file already using the following:

import type { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const page_name: string = req.body.name;
  const page_slug: string = req.body.slug;
  const page_directory: string = req.body.directory;
  const page_layout: string = req.body.layout;
  const components: Array<string[]> = req.body.components;

  const page_data = {
    layout: page_layout,
    components,
  };

  fs.writeFile(
    `Pages/${page_name}`,
    JSON.stringify(page_data),
    function (error) {
      if (error) throw error;

      return res.json({
        message: "File has been created",
      });
    }
  );
}

e.g. if I where to call this api and pass in a name of about it would create a about.tsx file, but the content would obviously be empty.

How would I go about using a template file and replacing said content with content coming from the api?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!