The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Serve A Text File From The Next.js API

TODO: Pull subtitle into page object

Create a file at:

code_start_default_section code_end_default_section

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Serve A Text File From The Next.js API

-- p

Create a file at:

-- code/

./pages/api/serve-file.js

-- /code

-- p

This is the sample you can work from:

-- code/

import fs from 'fs'

export default function handler(req, res) {
  const { file_key } = req.query

  // Note this hangs if the file isn't there.
  // not worred about it,
  // but something to keep in minde for future reference
  //
  // make sure there is only one directly and one file
  const file_parts = file_key.split('/')
  if (file_parts.length === 2) {
    const file_path = `./pages/${file_key}`
    if (fs.existsSync(file_path)) {
      const file_data = fs.readFileSync(file_path, `utf-8`)
      res.status(200).json(file_data)
    } else {
      res.status(404)
    }
  } else {
    res.status(404)
  }
}

-- /code


-- categories
-- Next.js

-- metadata
-- date: 2022-01-25 20:27:51
-- id: 24dlzhmz
-- status: scratch
-- type: post
-- SCRUBBED_NEO: false
-- site: aws