Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

express - Using Multer for file uploads, do you know why I get a 404 GET error when I am making a POST request and trying to render the response on my server?

I am learning Multer and trying to make the most basic app i can to start. I get a 404 GET error when I try to render the response. Here is my code. Thanks for your help.

const express = require('express')
const multer = require('multer')
const path = require('path');

const app = express()

const port = 3001

app.use(express.static(__dirname + 'uploads/'));

app.listen(port, () => {
    console.log(`listening on ${port}`)
})

var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'uploads/');
     },
    filename: function (req, file, cb) {
        cb(null , file.originalname);
    }
});

var upload = multer({ storage: storage })
//request
app.post('/upload-profile-pic', upload.single('any_name_you_want'), (req, res) => {
    try {
        console.log(req.file.path)
      res.send(`<img src="${req.file.path}" width="500">`);
    }catch(err) {
      res.send(400);
    }
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...