0

I'm new to node.js and coding , I'm trying to upload images to node.js , I follow this , upload binary file to node , test by POSTMAN , but when I choice body/binary in POSTMAN , URL type http://localhost:3000/ ,with api name or not , node always reply

SyntaxError: Unexpected token �

I check Many answers , try for 6 hours , can't figure it out , please help :(

node app.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var multer  =  require('multer');
binary = require('binary');
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.use(function(req, res, next) {
     var data = new Buffer('');
     req.on('data', function(chunk) {
         data = Buffer.concat([data, chunk]);
    });
    req.on('end', function() {
        req.rawBody = data;
        next();
    });
});
/////////////

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.js

var mongoose = require('mongoose');
var mongo = require('mongodb');
var uri = "mongodb://xxxx:[email protected]:61365/aweitest";
mongoose.connect(uri);
var multer  =  require('multer');
// we're connected!
var db = mongoose.connection.db;
var BSON = require('bson').BSONPure;
var body = require('body-parser');
var fs = require('fs');

db.on('error', console.error.bind(console, 'connection errrrrrrrror:'));
//db = mongoose.connection.db;
db.once('open', function() {
    console.log("mongodb is connected!!");
});


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"});
            }
        });
    });
};

exports.viewByFile = function (req, res){
  file = req.params.file;
     var dirname = "/Node/file-upload";
       var img = fs.readFileSync(dirname + "/uploads/" + file);
          res.writeHead(200, {'Content-Type': 'image/jpg' });
               res.end(img, 'binary');
  };

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)
20
  • Please format and clean up your code Commented Feb 25, 2016 at 7:07
  • @Oleander sorry , working on it, 3 mins Commented Feb 25, 2016 at 7:11
  • 1
    and why you are not using multer ,it is already been imported in your route.js Commented Feb 25, 2016 at 9:15
  • 1
    it will make a file object in req object itself Commented Feb 25, 2016 at 9:17
  • 1
    clear the cache of postman and then try it or just download an extension of advance restclient in chrome and test it from there Commented Feb 25, 2016 at 9:20

1 Answer 1

1

clear the cache of postman and then try it or just download an extension of advance rest client in chrome and test it from there

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.