Multer图片上载错误:意外令牌-JSON在位置0,语法错误:意外令牌#在JSON中位置0
请帮助我摆脱困境.这是我的uploadRouter.js,其中我尝试使用multer模块从POSTMAN上传图像文件
Please help me to get out of this. This is my uploadRouter.js in which I am trying to upload image file from POSTMAN using multer module
const express = require('express');
const mongoose = require('mongoose');
const autheticate = require('../authenticate');
const multer = require('multer')
const bodyParser = require('body-parser');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/images');
},
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const imageFileFilter = (req, file, cb) => {
if(!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('You can upload only image files!'), false);
}
cb(null, true);
};
const upload = multer({ storage: storage, fileFilter: imageFileFilter});
//const uploadImgFile = multer().single('imageFile');
const uploadRouter=express.Router();
uploadRouter.use(bodyParser.json());
uploadRouter.route('/')
.get(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
res.statusCode = 403;
res.end('GET operation not supported on /imageUpload');
})
.post(autheticate.verifyUser,autheticate.verifyAdmin,upload.single('imageFile'),(req,res,next)=>{
res.statusCode=200;
res.setHeader('Content-Type','application/json');
res.end('End');
})
.put(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
res.statusCode = 403;
res.end('GET operation not supported on /imageUpload');
})
.delete(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
res.statusCode = 403;
res.end('GET operation not supported on /imageUpload');
})
module.exports = uploadRouter;
我已将请求消息的正文类型从POSTMAN设置为form-data
,
但是当我上传图片时更改了正文格式后,POSTMAN中给出了以下错误
I have set the body type of request message to form-data
from POSTMAN,
But after changing body-format when I am uploading the image, the following error is given in POSTMAN
<body>
<h1>Unexpected token - in JSON at position 0</h1>
<h2>400</h2>
<pre>SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\types\json.js:157:10)
at parse (F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\types\json.js:83:15)
at F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:224:16)
at done (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)</pre>
</body>
</html>
POSTMAN中包含两个标头:
1.内容类型:"application/json"
2.身份验证:承载[[TOKEN]]
请我无法理解该错误,我是node.js的新手.请帮我解决它
Two headers are contained in POSTMAN:
1. Content-Type: 'application/json'
2. Authentication: bearer [[TOKEN]]
Please I am unable to understand the error, I am very new to node.js. Please help me through it
当您将Content-Type作为application/json传递并上传其content-type不是json的文件时,会发生此问题. 删除此标头,因为它不是必需的.
This issue occurs as you are passing Content-Type as application/json and uploading a file whose content-type is not json. Remove this header as it is not required.