简体   繁体   中英

Defining a new promise in express.js

I am working on posting an image to the Google Vision API, and returning the labels of what the picture contains.

I am using express.js and the Google Cloud Vision module, and I am having trouble creating a promise from the results of the Google Vision API. When I post an image to the API, it will return an array of labels. How do I ensure that after the vision API helper function runs that I store those results and return it to my react front end?

Server.js

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
const multer  = require('multer');
const upload = multer({ dest: './uploads' });
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads')
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + '-' + file.originalname)
    }
})
const google = require('../helpers/googleVisionAPI.js');

//Hitting file limits on size, increased to 50mb
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
app.use(require('express-promise')());

app.post('/upload', upload.single('image'), (req, res) => {
  google.visionAPI(req.file.path).then(labels => {
    //SEND LABELS BACK TO REACT FRONTEND
    console.log(labels);
    res.send(labels);
  })
});

app.listen(8080);

GoogleVisionAPI.js helper

// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();

let visionAPI = function(image) {
  client
    .labelDetection(image)
    .then(results => {
      // Pull all labels from POST request
      const labels = [];
      results[0].labelAnnotations.forEach(function(element) {
        labels.push(element.description);
      });
      return labels;
    })

    // ERROR from Cloud Vision API
    .catch(err => {
      console.log(err);
      res.end('Cloud Vision Error:' , err);
    });
}

exports.visionAPI = visionAPI;

Can someone have the Vision API run, and then store the returned results to a label variable to have sent back to my react frontend?

Your GoogleVisionAPI.js can be shortened to just:

// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();

exports.visionAPI = async (filePath) => {
  const results = await client.labelDetection(filePath)
  const labels = results[0].labelAnnotations.map(element => element.description)
  return labels
}

Then your Server.js where you call out to your helper can just be:

app.post('/upload', upload.single('image'), async (req, res) => {
  const labels = await google.visionAPI(req.file.path);
  console.log(labels)
  res.json(labels)
});

Error handling has been omitted and should be delegated to error handling middleware .

Above code is untested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM