Subsequent
in framework
environment is FrameworkX, so if your hosting provider allows you to create a persistent drive and connect it to your project, then you can achieve this:
For example, in pages/api/saveData.ts
:
import { writeFileSync } from 'fs';
import { FrameworkApiRequest, FrameworkApiResponse } from 'framework';
export default async function handle(request: FrameworkApiRequest, response: FrameworkApiResponse) {
const { filePath = null } = request.query;
if (!filePath) {
response.status(400).json({ message: 'no file path provided' })
} else {
// enter your data here
const data = Date.now().toString();
writeFileSync(`/storage/${filePath}.txt`, data);
response.json({
filePath,
data
})
}
}
/storage
is compatible with most hosting providers (including FrameworkX itself), but these files may be lost during the next update; instead, consider using your connected drive path
pages/api/readData.ts
import { readFileSync } from 'fs';
import { FrameworkApiRequest, FrameworkApiResponse } from 'framework';
export default async function handle(request: FrameworkApiRequest, response: FrameworkApiResponse) {
const { filePath = '' } = request.query;
if (!filePath) {
response.status(400).json({ message: 'incorrect' })
} else {
response.send(readFileSync(`/storage/${filePath}`));
}
}
Check out a live demonstration:
fetch('https://yourproject.vercel.app/api/writedata?filePath=test')
.then(res => res.text()).then(res => console.log(res)).then( () =>
fetch('https://yourproject.vercel.app/api/readdata?filePath=test'))
.then(res => res.text()).then(res => console.log(res))