使用node.js和Express,文件上传系统无法获取/错误

时间:2018-03-29 02:19:46

标签: node.js express web

我正在为我的一个类开发文件上传和转换项目(Excel到csv文件),我不明白为什么我一直收到这个错误。我只需要这个工作,以便我可以继续进行项目的转换部分。我有用express,multer和uuvid安装的node.js,我也在Windows 10上。这是代码:

const port = process.env.PORT || 3000;
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const uuidv4 = require('uuid/v4');
const path = require('path');

    // configure storage
    const storage = multer.diskStorage({
      destination: (req, file, cb) => {
        /*
          Files will be saved in the 'uploads' directory. Make
          sure this directory already exists!
        */
        cb(null, './uploads');
      },
      filename: (req, file, cb) => {
        /*
          uuidv4() will generate a random ID that we'll use for the
          new filename. We use path.extname() to get
          the extension from the original file name and add that to the new
          generated ID. These combined will create the file name used
          to save the file on the server and will be available as
          req.file.pathname in the router handler.
        */
        const newFilename = `${uuidv4()}${path.extname(file.originalname)}`;
        cb(null, newFilename);
      },
    });
    // create the multer instance that will be used to upload/save the file
    const upload = multer({ storage });

    const app = express();

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));

    app.post('/', upload.single('selectedFile'), (req, res) => {
      /*
        We now have a new req.file object here. At this point the file has been saved
        and the req.file.filename value will be the name returned by the
        filename() function defined in the diskStorage configuration. Other form fields
        are available here in req.body.
      */
      res.send();
    });

    app.listen(port, () => console.log(`Server listening on port ${port}`));

我从博客中获取此代码,可在此处找到: https://blog.stvmlbrn.com/2017/12/17/upload-files-using-react-to-node-express-server.html

我注意到其他.js文件包含app.get函数,所以我认为错误是我没有这段代码。我不知道的是放在哪里,或放在里面的东西。谢谢!

0 个答案:

没有答案