Saturday, 20 May 2017

Router.use() requires middleware function but got a string?

I have looked on the forums and have tried fixing everything I could, but still can't seem to get it to work. I am wanting to use router instead of having to use app.get. I have another project in which I am doing the same thing and it works just fine. So I am a little confused as to why this one isn't working. Thank you very much.

Here is my app.js:

var express = require("express");
var app = express();
var indexRoutes = require("./routes/index.js");

app.use("view engine", "ejs");
app.use(express.static(__dirname + "/public"));

app.use("/", indexRoutes);

app.listen(process.env.PORT, process.env.IP, function() {
    console.log("server started on port : " + process.env.PORT);
});

Here is the route I am using:

var express = require("express");
var router = express.Router();
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './uploads');
    },
    filename: function(req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now());
    }
});
var upload = multer({storage: storage}).single('userPhoto');


router.get("/", function(req, res) {
   res.render("index"); 
});

router.post("/uploads", function(req, res) {
    upload(req, res, function(err) {
        if(err) {
            return res.send("error uploading file");
        }
        res.end("file is uploaded");
    });
});

module.exports = router;



via Taylor Austin

No comments:

Post a Comment