Next.js Discord

Discord Forum

Fetch is not working, am I calling it wrong?

Unanswered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
Hello everyone, i'm trying to do a server-side call and change some data using pandoc and fs.

I've included my file structure for reference.
I am doing this in the following file textConversion.ts:
import { NextApiRequest, NextApiResponse } from "next";
import fs from 'fs'
import path from 'path';
import nodePandoc from "node-pandoc";


export default async (req: NextApiRequest  , res: NextApiResponse ) => {
    
    const filePath = path.join(process.cwd(), 'public', 'message.md')  

    fs.readFile(filePath, 'utf8', (err, data) => {
        if (err){
          console.error('Error reading file:', err)
          return;
        }
        const src = data;
        try {
          nodePandoc(src, '-f markdown -t latex', (err: string, latexResult: any) => {
            if (err){
              console.error(err)
              return res.status(500).json({ error: 'Failed to convert Markdown to LaTeX' })
            }
            nodePandoc(latexResult, '-f latex -t html5', (err: string, htmlResult: any) => {
              if(err){
                console.error(err)
                return res.status(500).json({error: 'Failed to convert LaTeX to HTML'})
              }
              res.status(200).json({ html: htmlResult})
            })
          })
        } catch (error){
          console.error('Error reading file:', error)
          return res.status(500).json({ error: 'Failed to read file'})
        }
      })
}

I am then fetching it here textConversionFetch.ts:
export async function textConversion() {
    const response = await fetch('/api/textConversion', {
      method: 'GET'
    });
  
    if (!response.ok) {
      throw new Error('Failed to fetch text conversion');
    }
  
    const data = await response.json();
    return data.html;
  }

4 Replies

American ChinchillaOP
I'm getting a 404 error, but I feel like the file path is correct.
@American Chinchilla Hello everyone, i'm trying to do a server-side call and change some data using pandoc and fs. I've included my file structure for reference. I am doing this in the following file textConversion.ts: typescript import { NextApiRequest, NextApiResponse } from "next"; import fs from 'fs' import path from 'path'; import nodePandoc from "node-pandoc"; export default async (req: NextApiRequest , res: NextApiResponse ) => { const filePath = path.join(process.cwd(), 'public', 'message.md') fs.readFile(filePath, 'utf8', (err, data) => { if (err){ console.error('Error reading file:', err) return; } const src = data; try { nodePandoc(src, '-f markdown -t latex', (err: string, latexResult: any) => { if (err){ console.error(err) return res.status(500).json({ error: 'Failed to convert Markdown to LaTeX' }) } nodePandoc(latexResult, '-f latex -t html5', (err: string, htmlResult: any) => { if(err){ console.error(err) return res.status(500).json({error: 'Failed to convert LaTeX to HTML'}) } res.status(200).json({ html: htmlResult}) }) }) } catch (error){ console.error('Error reading file:', error) return res.status(500).json({ error: 'Failed to read file'}) } }) } I am then fetching it here textConversionFetch.ts: typescript export async function textConversion() { const response = await fetch('/api/textConversion', { method: 'GET' }); if (!response.ok) { throw new Error('Failed to fetch text conversion'); } const data = await response.json(); return data.html; }
you are using app router, so you should create the route handler at /api/textConversion/route.ts
https://nextjs.org/docs/app/building-your-application/routing/route-handlers