我想将图像从android上传到node.js,关注this one,我现在可以从网上查看图片,但始终上传图片
SyntaxError:意外的令牌�
如果我选择1.5mb文件则显示
错误:请求实体太大
我发现this,似乎我必须将文件转换为JSON
但我虽然已经使用这三个来加载大文件并将其转换为JSON
app.use(bodyParser.json());
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true }))
在这里坚持了好几天,请帮忙
码
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http');
app.use(bodyParser.json());
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies.
songs = require('./routes/route');
var jsonParser = bodyParser.json();
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/songs',songs.findAll);
app.get('/findById/:id',songs.findById);
app.post('/songs',songs.addSong);
app.put('/songs/:id',songs.updateSong);
app.delete('/songs/:id',songs.deleteSong);
app.post('/upload',songs.updateSong);
app.get('/upload/:file',songs.viewByFile);
路线代码
exports.upload = function(req, res) {
console.log(req.files.image.originalFilename);
console.log(req.files.image.path);
fs.readFile(req.files.image.path, function (err, data){
var dirname = "/Node/file-upload";
var newPath = dirname + "/uploads/" + req.files.image.originalFilename;
fs.writeFile(newPath, data, function (err) {
if(err){
res.json({'response':"Error"});
}else {
res.json({'response':"Saved"});
}
});
});
};
错误
SyntaxError: Unexpected token �
at parse (c:\Users\awei\node_modules\body-parser\lib\types\json.js:83:15)
at c:\Users\awei\node_modules\body-parser\lib\read.js:116:18
at invokeCallback (c:\Users\awei\node_modules\raw-body\index.js:262:16)
at done (c:\Users\awei\node_modules\raw-body\index.js:251:7)
at IncomingMessage.onEnd (c:\Users\awei\node_modules\raw-body\index.js:308:7)
at emitNone (events.js:80:13)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:906:12)
at nextTickCallbackWith2Args (node.js:475:9)
at process._tickCallback (node.js:389:17)
答案 0 :(得分:0)
如果您使用像邮递员这样的扩展程序,请确保Content-Type是多部分/表单数据,而不是请求标头中的application / json。