javascript - Save image as base64 and return it with rout -


i'm kinda new working nodejs , first time creating api, i'm uploading image angular , ng-file-upload server, , idea api receives file, encodes base64 , gives me object path image display on client side.

config.js

'use strict'  const express = require('express'); const bodyparser = require('body-parser'); const app = express(); const multer = require('multer'); const fs = require('fs-extra');  app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json());  app.use(function(req, res, next) {   res.header("access-control-allow-origin", "*");   res.header("access-control-allow-headers", "origin, x-requested-with, content-type, accept, authorization");   res.header("access-control-allow-methods", "get,head,post,put,delete");   next(); });  app.use('/uploads', express.static("./uploads")); app.use(bodyparser.json());  var storage = multer.diskstorage({ //multers disk storage settings   destination: function (req, file, cb) {     let ind = file.mimetype.indexof('/');     let type = file.mimetype.slice(0, ind);     let path = `./uploads/${type}`;     fs.mkdirssync(path);     cb(null, path);   },   filename: function (req, file, cb) {     var datetimestamp = date.now();     var ind = file.originalname.indexof('.');     var filename = file.originalname.slice(0 , ind);     cb(null, filename+ '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1])   } });  var upload = multer({ //multer settings               storage: storage             }).single('avatar', 12); /** api path upload files */  app.post('/upload', function(req, res) {   upload(req,res,function(err){   if(err){    res.json({error_code:1,err_desc:err});    return;   }     res.json(req.file);     console.log(req.file)   }); }); 

how can it, i'm using express , multer upload files. idea "data:image/png;base64,ivborw0kggoaaaansuheugaaagqaaabkcayaaabw4pvuaaaacxbiwxmaaapoaaad6ag1e1jraaagaeleqv"


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -