如何在nodejs中存储req.body.state
how to store req.body.state in nodejs
这是节点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: {//need req.body.input here //}}
先做console.log(req.body)
,如果看不懂数据,检查3件事。
- 检查服务器路由器 HTTP 方法是否为
post
、put
、delete
之一
- 检查浏览器端 HTTP 是否调用正确的方法和参数 ex>
axios.post(url, data, config)
- 检查正文解析器是否附加在同一上下文中。例如,如果你做了
app.use(bodyParser())
,所有子路由都应该附加到 app
以引用 req.body
如果此检查列表不适合您,请分享客户端代码和服务器端路由器代码以了解详细信息.
为什么不执行这样的代码?
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();
}
}
);
});
前端
//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 returns一个循环结构数据对象,直接使用会抛出错误,所以“stringify (response.results[0].outputs[4].data.regions, null, 2)" 将其转换为常规 json 对象。
人口统计模型适用于多张面孔,因此如果图片中有多张面孔,那么您可以从 "response.results[0].outputs[4].[=27= 进行跟进]"
此外,在 server.js 中一定要包含
//server.js
...
const posts = require('./demographics.js')
const PORT = process.env.PORT || 5000
app.post('/data', posts.demographics)
...
这是节点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: {//need req.body.input here //}}
先做console.log(req.body)
,如果看不懂数据,检查3件事。
- 检查服务器路由器 HTTP 方法是否为
post
、put
、delete
之一
- 检查浏览器端 HTTP 是否调用正确的方法和参数 ex>
axios.post(url, data, config)
- 检查正文解析器是否附加在同一上下文中。例如,如果你做了
app.use(bodyParser())
,所有子路由都应该附加到app
以引用req.body
如果此检查列表不适合您,请分享客户端代码和服务器端路由器代码以了解详细信息.
为什么不执行这样的代码?
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();
}
}
);
});
前端
//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 returns一个循环结构数据对象,直接使用会抛出错误,所以“stringify (response.results[0].outputs[4].data.regions, null, 2)" 将其转换为常规 json 对象。
人口统计模型适用于多张面孔,因此如果图片中有多张面孔,那么您可以从 "response.results[0].outputs[4].[=27= 进行跟进]"
此外,在 server.js 中一定要包含
//server.js
...
const posts = require('./demographics.js')
const PORT = process.env.PORT || 5000
app.post('/data', posts.demographics)
...