问题描述
这是节点js中服务器的后端部分
This is the backend section of the server in node js
stub.PostWorkflowResults(
{
workflow_id: "Demographics",
inputs: [
{data: {//need req.body.input here //}}
]
},
metadata,
(err, response) => {
if(response){
console.log(response)
}else {
console.log(err)
}
我使用了bodyparser.需要将req.body.input放在对象{data:{//需要req.body.input此处//}}
I used bodyparser. What needed is to put req.body.input inside the object {data: {//need req.body.input here //}}
推荐答案
FrontEnd
//App.js
import axios from 'axios'
import {useEffect, useState} from 'react'
import './App.css';
function App() {
const [something, setSomething] = useState({})
const url = "http://localhost:5000/data"
const input = "https://static.independent.co.uk/s3fs-public/thumbnails/image/2015/06/06/15/Chris-Pratt.jpg"
const func = async () => {
const {data} = await axios.post(url, {input : input})
setSomething(data)
}
useEffect(() => {
func()
}, [])
return (
<div className="App">
<h1>APP WORKING</h1>
{console.log("something : ",something)}
</div>
);
}
export default App;
后端
//demographics.js
const {ClarifaiStub, grpc} = require("clarifai-nodejs-grpc");
const stringify = require('json-stringify-safe');
const demographics = (req,res) => {
const stub = ClarifaiStub.grpc()
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key #ClarifaiKey#");
stub.PostWorkflowResults(
{
workflow_id: "Demographics",
inputs: [
{data: {image: {url: req.body.input}}}
]
},
metadata,
(err, response) => {
if(response){
console.log("AGE :", response.results[0].outputs[4].data.regions[0].data.concepts[0])
here --> const data = stringify(response.results[0].outputs[4].data.regions, null, 2)
res.send(data)
}
else {
console.log(err)
res.status(400)
}
}
)
}
module.exports = {
demographics: demographics
}
我对'json-stringify-safe'包不是很有信心,但是据我所知,nodejs-grpc clarifai返回一个圆形结构数据对象,当直接使用它时会抛出错误,因此"stringify(response.results [0] .outputs [4] .data.regions,null,2)"将其转换为常规的json对象.
I am not very confident about 'json-stringify-safe' package but from what I could make out, nodejs-grpc clarifai returns a circular structure data object which would throw an error when used directly so "stringify(response.results[0].outputs[4].data.regions, null, 2)" converts it into a regular json object.
人口统计模型适用于多张面孔,因此,如果图片中有多张面孔,则可以从"response.results [0] .outputs [4] .data.regions"
Demographics model works for multiple faces so if there are multiple faces in the picture then you can follow up from "response.results[0].outputs[4].data.regions"
此外,请确保在server.js中包含
Also, in server.js be sure to include
//server.js
...
const posts = require('./demographics.js')
const PORT = process.env.PORT || 5000
app.post('/data', posts.demographics)
...
这篇关于如何在Node.js中存储req.body.state的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!