RUNNb
RUNNb
Explore posts from servers
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I am trying to persist image on volume. RAILWAY_VOLUME_MOUNT_PATH =/images I've tried to implement the change for the railway volume but without success. This is the api route for local storage on /public/upload_images
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.cwd(), "/public/upload_images");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.cwd(), "/public/upload_images");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
38 replies
RRailway
Created by RUNNb on 5/22/2023 in #✋|help
Can't access api url (nestjs project)
So on my local env if I access localhost:port/api I can see api swagger documentation. Also I can do any request. When deployed on railway I get no errors, I see app running logs but can't access api documentation page or do any request. Any ideas on what is possibly going on?
17 replies