Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Node Express 404 error when trying to load image uploaded by multer

Im trying to upload images with multer. Everything has worked before on localhost fine. Images get uploaded and i can view them from the url link the code provides. However once i have uploaded it on a server im starting to get this error : "Cannot GET /marketplace-api/rest-api/image/1650484814178.jpg".

I have not provided link to server/domains to the question in case people say thats the problem.

Here is my code:

const express = require('express');
const app = express();
const multer = require("multer");
const path = require("path");
const db = require('./db');
const cors = require('cors');
const port = 5000;
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true , limit: '50mb'}));
app.use(cors());



app.use("/image", express.static("image")); 
let imageName = ""; 
const storage = multer.diskStorage({
  destination: path.join("./image"), 
  filename: function (req, file, cb) {
    imageName = Date.now() + path.extname(file.originalname);
    cb(null, imageName);
  },
});
const upload = multer({
  storage: storage,
  limits: { fileSize: 3000000 },
}).single("myImage");
app.post("/marketplace-api/rest-api/upload-image", (req, res) => {
  upload(req, res, (err) => {
    if (err) {
      console.log(err);
    } else {
      return res.status(201)
      .json({ url: "http://link to domains/marketplace-api/rest-api/image/" + imageName }); 
    }
  });
});
app.listen(port, () => {
  console.log("server run in port", port);
});
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

ok, few things, 1. you dont have any get functions, which means you cant post to this location, i suggest doing this:

app.get('/', (req, res) => {
    res.sendFile(__dirname + "/index.html");
});

and create a file called index.html, go on your site (localhost), and it should work, after that add a form in the html file, it should look something like this

<form action = "/marketplace-api/rest-api/upload-image" method = "POST">
    <input type = "file" name="image">
    <input type = "submit">
</form>

this will add a basic form where you can upload your photo. there are a few more stuff to add / change, i will fix everything and make it a repo then send it here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda