U
U
UFOLOG65ru2021-02-17 10:17:52
Node.js
UFOLOG65ru, 2021-02-17 10:17:52

Multer not working from module with multiple require, how to fix?

Index.js

var express = require("express");
var app = express();
var cors = require("cors");
app.use(cors());

require("./admin/front.js")(app);
require("./admin/back.js");
require("./admin/message.js")(app);
require("./admin/upload.js")(app);
require("./upload.js");
require("./admin/course.js")(app);
require("./admin/validate.js");
require("./admin/XML.js")(app);
require("./admin/news.js")(app)



app.listen(10100,  () => {
  console.log('Back app listening on port 10100!');
});


news.js
module.exports = function (app){

const multer  = require("multer");
const fileFilter = (req, file, cb) => {
  const allowedTypes = ["image/jpeg", "image/jpg", "image/png"];
  if (!allowedTypes.includes(file.mimetype)) {
    const error = new Error("Incorrect file");
    error.code = "INCORRECT_FILETYPE";
    return cb(error, false)
  }
  cb(null, true);
}

const upload = multer({
  dest: './uploads',
  fileFilter,
  limits: {
    fileSize: 5000000
  }
});

app.post('/news/upload', upload.single('file'), (req, res) => {
  res.json({ file: req.file });
});

app.use((err, req, res, next) => {
  if (err.code === "INCORRECT_FILETYPE") {
    res.status(422).json({ error: 'Only images are allowed' });
    return;
  }
  if (err.code === "LIMIT_FILE_SIZE") {
    res.status(422).json({ error: 'Allow file size is 500KB' });
    return;
  }
});
}


news.vue

<template>
  <div class="file">
    <form @submit.prevent="onSubmit" enctype="multipart/form-data">
      <div class="fields">
        <label>Upload File</label><br />
        <input type="file" ref="file" @change="onSelect" />
      </div>
      <div class="fields">
        <button>Submit</button>
      </div>
      <div class="message">
        <h5>{{ message }}</h5>
      </div>
    </form>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  name: "FileUpload",
  data() {
    return {
      file: "",
      message: ""
    };
  },
  methods: {
    onSelect() {
      const allowedTypes = ["image/jpeg", "image/jpg", "image/png"];
      const file = this.$refs.file.files[0];
      this.file = file;
      if (!allowedTypes.includes(file.type)) {
        this.message = "Filetype is wrong!!";
      }
      if (file.size > 500000) {
        this.message = "Too large, max size allowed is 500kb";
      }
    },
    async onSubmit() {
      const formData = new FormData();
      formData.append("file", this.file);
      try {
        await this.$axios.post(this.$back_server+"news/upload", formData);
        this.message = "Uploaded!!";
      } catch (err) {
        console.log(err);
        this.message = err.response.data.error;
      }
    }
  }
};
</script>


in the index everything is divided into modules to listen to one port, the rest of the modules are in order, did the file download, did not understand why the file does not come, although it is displayed in the console, by typing I managed to get to the bottom of this problem, if you remove all module connections from the app, it works, what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
UFOLOG65ru, 2021-02-18
@UFOLOG65ru

The problem solved itself by simply moving the connection for news to the top of the list of connections.
But if anyone can explain why the problem took place, I will not refuse to read)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question