I want to upload images from android to node.js , follow this one , I can view the image from web now , but upload image always
SyntaxError: Unexpected token �
and if I choice 1.5mb file it show
Error: request entity too large
I found this , seems like I have to convert file to JSON
but I though I already use this three to load big file and convert it into JSON
app.use(bodyParser.json());
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true }))
stuck here for days , help please
code
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);
route code
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"});
}
});
});
};
error
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)