简体   繁体   English

如何在Node.js中上传文件? 错误:无法发布/上传

[英]How to upload a file in Node.js ? Error: Cannot POST /upload

PROBLEM: 问题:

I click on submit in my upload.ejs and I get the error: "Cannot POST /upload" 我在我的upload.ejs中单击“提交”,但收到错误消息:“无法发布/上传”


SOLUTION: 解:

My problem was an architectural one more than a coding problem. 我的问题是架构问题,而不是编码问题。

I changed the structure of my project to solve the problem. 我更改了项目结构以解决该问题。

I created a new file called "upload.js" to put the router code in. 我创建了一个名为“ upload.js”的新文件来放入路由器代码。

I also moved the "upload.ejs" to the root of the project. 我还将“ upload.ejs”移到了项目的根目录。

I updated my "app.js" to take into account the new router file "upload.js". 我更新了“ app.js”,以考虑到新的路由器文件“ upload.js”。

I changed the form to use "upload.js". 我将表单更改为使用“ upload.js”。


CODE: 码:

upload.js upload.js

var express = require("express");
var router = express.Router();
var flash = require("connect-flash");

var firebase = require("firebase");

var multer = require("multer");
var upload = multer({dest:"./public/images/uploads/"});

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

router.post("/", upload.single("image"), function(req, res, next){

    if (req.file){
        console.log("Uploading file...");
        var image = req.file.filename;
    }
    else {
        console.log("No file uploaded");
        var image = "noimage.jpg";
    }

    var post = {
        title: req.body.title,
        section: req.body.section,
        image: image,
    }

    var section = req.body.section.toLowerCase();

    firebase.database().ref("posts/"+section).push(post);

    req.flash("success_msg", "Post Created");
    res.redirect("/upload");


});

module.exports = router;

upload.ejs upload.ejs

<form enctype="multipart/form-data" method="post" action="upload">

You are POSTing to /upload url but you the route that you register is POST /users/upload . 您正在POST到/upload url,但是您注册的路由是POST /users/upload Possibly you have to move the follwing function into the routes/index.js file: 可能您必须将以下功能移至route / index.js文件中:

router.post("/upload", upload.single("image"), function(req, res, next){

    if (req.file){
        console.log("Uploading file...");
        var image = req.file.filename;
    }
    else {
        console.log("No file uploaded");
        var image = "noimage.jpg";
    }

    var post = {
        title: req.body.title,
        section: req.body.section,
        image: image,
    }

    var postRef = fbRef.child("posts");

    postRef.push().set(post);

    req.flash("success_msg", "Post Created")
    res.redirect("/"+ req.body.section.toLowerCase());

});

You need to use multer module: 您需要使用multer模块:

server.js server.js

multer = require('multer');
app.use(multer({
        dest: './public/uploads/',
        rename: function (fieldname, filename) {
            return filename.replace(/\W+/g, '-').toLowerCase();
        }
}));

upload.js upload.js

router.post("/upload",function(req, res, next){
    console.log(req.file);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM