0

When I try to upload an image file on my server, I have this issue :

TypeError: Router.use() requires middleware function but got a Object

My form code :

    <form action="/test.html" method="post" enctype="multipart/form-data" name="monForm">
      <input type="file" name="avatar" />
      <input type="submit" value="Upload">
    </form>

My node code :


    var express = require("express");
    var url = require("url");
    var router = express.Router();
    var bcrypt = require("bcryptjs");
    var path = require("path");
    var http = require("http");
    var mysql = require("mysql");
    var nodeMailer = require("nodemailer");
    var multer = require("multer");
    var upload = multer({ dest: "uploads/" });
    
    router.post("/test.html", upload.single("avatar"), function (req, res, next) {
      //upload.single('avatar'),
      console.log("it's ok");
    
      res.render("test", {});
      // req.file is the `avatar` file
      // req.body will hold the text fields, if there were any
    });
    
    module.exports = router;

Thanks for your help !

Luis Paulo Pinto
  • 5,578
  • 4
  • 21
  • 35
  • Does this answer your question? [TypeError: Router.use() requires middleware function but got a Object](https://stackoverflow.com/questions/27465850/typeerror-router-use-requires-middleware-function-but-got-a-object) – Roljhon Jun 22 '21 at 15:37
  • It really depends on how you use that file inside your main file (or to put it in other words - how you use your router in `app`). Also it's very weird to use `test.html` as an endpoint - is that a static file that is served, or just a weird name? – Andrey Popov Jun 22 '21 at 15:38
  • test.html is a static file to test my code – Laurent Gevaert Jun 22 '21 at 15:54
  • Tthe code missed must be a thing like that Router.use() but what put inside ? – Laurent Gevaert Jun 22 '21 at 16:49

0 Answers0