I followed this tutorial to create a IPFS + Express app with Node.JS in Ubuntu 20.04.6: https://www.youtube.com/watch?v=RMlo9_wfKYU
I encountered the following error: Cannot POST /upload after I completed the form at the home page.
Here are my source codes:
// 1. app.js
const ipfsClient = require('ipfs-http-client');
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const ipfs = new ipfsClient({host: 'localhost', port: '5001', protocol: 'HTTP'});
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(fileUpload());
app.get('/', (req, res) => {
res.render('test');
});
app.get('/upload', (req, res) => {
const file = req.files.file;
const fileName = req.body.fileName;
const filePath = 'files/' + fileName;
file.mv(filePath, async(err) => {
if(err) {
console.log('Error: Failed to download file.');
return res.status(500).send(err);
}
const fileHash = await addFile(fileName, filePath);
fs.unlink(filePath, (err) => {
if(err) {
console.log(err);
}
});
res.render('upload', { fileName, fileHash });
})
});
const addFile = async(fileName, filePath) => {
const file = fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: fileName, content: file});
const fileHash = fileAdded[0].hash;
return fileHash;
}
app.listen(3000,'127.0.0.1', () => {
console.log('Server is running on port 3000')
})
// 2. test.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatitable" content="ie=edge">
<title>IPFS Video Upload</title>
</head>
<body>
<h1>Upload file to IPFS</h1>
<form action="/upload/" method="POST" enctype="multipart/form-data">
<label>Filename</label>
<input type="text" name="fileName">
<br><br>
<label>Upload file</label>
<input type="file" name="file">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
// 3. upload.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatitable" content="ie=edge">
<title>IPFS Video Upload</title>
</head>
<body>
<h1>Upload Success</h1>
<p>Name: <%= fileName %></p>
<p>Link: <a href="https://ipfs.io/ipfs/<%= fileHash %>"><%= fileHash %></a>></p>
</body>
</html>
Is there any possible solution to address the issue, as I do not know if it’s related to kubo?