我在 youtube 上克隆了这段代码,它有无法获取/上传错误。
我在谷歌上搜索了这个错误,很多人说它看起来像是 app.get('/upload') 丢失问题。
但我不知道如何修复此代码。
你能告诉我如何解决吗?
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.engine('ejs',require('ejs').__express);
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(fileupload());
app.get('/',(req,res)=>{
res.render('home');
});
**app.post('/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 the file');
return res.status(500).send(err);
}
const fileHash = await addFile(fileName, filePath);
fs.unlink(filePath, (err)=>{
if(err) console.log(err);
});
res.send(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;
};*
答案 0 :(得分:0)
由于为 /upload
方法指定了 POST
路由:
app.post('/upload', (req,res)=>{
没有 GET /upload
路线,这是您得到的错误。
因此在调用端点时,您应该将方法更改为 POST