问题
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)
}
I used bodyparser. What needed is to put req.body.input inside the object {data: {//need req.body.input here //}}
回答1:
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;
Backend
//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
}
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.
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"
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)
...
回答2:
Why not implementing the code like that?
app.post('/', function(req, res){
stub.PostWorkflowResults(
{
workflow_id: "my-custom-workflow",
inputs: [
{
data: {
image: {
url: req.body.input // guessing that your input is a url
}
}
}
]
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post workflow results failed, status: " + response.status.description);
}
// Since we have one input, one output will exist here.
const result = response.results[0]
// One output is present for each model in the workflow.
for (const output of result.outputs) {
console.log("Predicted concepts for model: " + output.model.name);
for (const concept of output.data.concepts) {
console.log("\t" + concept.name + " " + concept.value);
}
console.log();
}
}
);
});
回答3:
Do console.log(req.body)
first, if you can't read the data, check 3 things.
- Check if server router HTTP method is one of
post
,put
,delete
- Check if browser side HTTP calls right method and argument ex>
axios.post(url, data, config)
- Check if body parser is attached in same context. For example, if you did
app.use(bodyParser())
, all sub routes should be attached intoapp
to referreq.body
If this check list doesn't work for you, please share client-side code and server-siderouter code for details.
来源:https://stackoverflow.com/questions/65589339/how-to-store-req-body-state-in-nodejs