ENOENT:没有这样的文件或目录. [英] ENOENT: no such file or directory .?

查看:229
本文介绍了ENOENT:没有这样的文件或目录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是发布数据和文件时出现的错误. 我遵循了"academind"教程来构建Restful API服务,我也一直在寻找此类错误的答案,但对我没有任何帮助.

This is error which am getting while post data and file. I have followed 'academind' tutorial for building Restful API services, also i have been searching answer for this type of errors but nothing works for me.

我使用"multer"上传文件

Am using "multer" to upload file

该文件夹中的文件夹上载"可用,但显示为

The folder 'uploads' available in the folder but it shows

ENOENT:无此类文件或目录,打开'D:\ project \ uploads \ 2018-01-24T07:41:21.832Zcheck.jpg'"

ENOENT: no such file or directory, open 'D:\project\uploads\2018-01-24T07:41:21.832Zcheck.jpg'"

app.js

const express = require("express");
const app = express();
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const productRoutes = require("./api/routes/products");

mongoose.connect('',
(err)=>{
    if(err){console.log(err)}
    else{console.log('DB Connected')}
})
mongoose.Promise = global.Promise;

app.use(morgan("dev"));
app.use('/uploads', express.static('uploads'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  if (req.method === "OPTIONS") {
    res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
    return res.status(200).json({});
  }
  next();
});

// Routes which should handle requests
app.use("/products", productRoutes);

app.use((req, res, next) => {
  const error = new Error("Not found");
  error.status = 404;
  next(error);
});

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
    }
  });
});

module.exports = app;

product.js

product.js

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const multer = require('multer');

const storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, './uploads/');
  },
  filename: function(req, file, cb) {
    cb(null, new Date().toISOString() + file.originalname);
  }
});

const fileFilter = (req, file, cb) => {
  // reject a file
  if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
    cb(null, true);
  } else {
    cb(null, false);
  }
};

const upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 5
  },
  fileFilter: fileFilter
});

router.post("/", checkAuth, upload.single('productImage'), (req, res, next) => {
  const product = new Product({
    _id: new mongoose.Types.ObjectId(),
    name: req.body.name,
    price: req.body.price,
    productImage: req.file.path 
  });
  product
    .save()
    .then(result => {
      console.log(result);
      res.status(201).json({
        message: "Created product successfully",
        createdProduct: {
            name: result.name,
            price: result.price,
            _id: result._id,
            request: {
                type: 'GET',
                url: "http://localhost:3000/products/" + result._id
            }
        }
      });
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });
});

module.exports = router;

product.js中的

推荐答案

:

new Date().toISOString()之后添加replace(),将:" 更改为可接受的字符.

in product.js:

After new Date().toISOString() add replace() to change ":" to an accepted character.

Windows操作系统不接受带有:"

Windows OS doesn't accept files with a ":"

Youtube上的人正在使用MAC OS

The person on Youtube is using MAC OS

例如

new Date().toISOString().replace(/:/g, '-')

这篇关于ENOENT:没有这样的文件或目录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆