Hello, Everyone. I am the new to IPFS, especially Helia.
The problem is that my code runs well but I can’t find the url of uploaded files.
My code is following.
app.js
import { unixfs as createUnixfs } from "@helia/unixfs";
import Path from "path";
import fs from "fs";
import { createHelia } from "helia";
import { exec } from "child_process";
import express from 'express';
import { raceSignal } from 'race-signal';
const router = express.Router();
const directory = "./DevFolio";
const helia = await createHelia();
const unixfs = createUnixfs(helia);
let files = 0;
let totalFileCount = 0;
const startTime = Date.now();
const addDir = async function (dir) {
const dirents = await fs.promises.readdir(dir, { withFileTypes: true });
let rootCid = await unixfs.addDirectory();
await Promise.all(dirents.map(async dirent => {
const path = Path.join(dir, dirent.name);
console.log(path);
const cid = dirent.isDirectory() ?
await addDir(path) :
await unixfs.addFile({ content: fs.createReadStream(path, { highWaterMark: 16 * 1024 }) });
rootCid = await unixfs.cp(cid, rootCid, dirent.name);
files++;
console.log(files);
console.log(rootCid);
console.log(`${((Date.now() - startTime) / 1000).toFixed(1)}s: ${files} Files, ${(files * 100 / totalFileCount).toFixed(2)}%`);
}));
return rootCid;
};
router.post('/', async (req, res) => {
try {
totalFileCount = await new Promise(
resolve => exec(`find ${directory} | wc -l`, (_, result) => resolve(+result))
);
console.log(totalFileCount)
// const signal = raceSignal(100000); // create a signal with a 15 second timeout
try {
const rootCid = await addDir(directory); // pass the signal to the addDir function
console.log(rootCid);
res.status(200).json({ message: "success" });
} catch (error) {
// handle the AbortError if the operation is aborted
if (error.name === 'AbortError') {
console.log('The addDir operation was aborted');
// you can return a default value, retry the operation, or do something else here
res.status(500).json({ error: 'The operation was aborted' });
} else {
// handle other errors
console.error(error);
res.status(500).json({ error: error.message || "Internal Server Error" });
}
}
} catch (error) {
console.error(error);
res.status(500).json({ error: error.message || "Internal Server Error" });
}
});
export default router;'''
server.js
// require('dotenv').config();
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import http from 'http';
import router from './app.js'
const app = express();
const httpServer = http.createServer(app);
// express init
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.use(cookieParser())
// api routes
app.use('/', router);
const port = process.env.PORT || 5000;
httpServer.listen(port, () => {
console.log('listening on *:' + port);
});
The CID of the result is bafybeicvyjov6kq3ozzwnmdqkjopscwijrc3a43sgrxaz44irse77tscui .
So, I tried to connect to /ipfs/bafybeicvyjov6kq3ozzwnmdqkjopscwijrc3a43sgrxaz44irse77tscui/ but 504 gateway error occurs. How can I fix it?