Javascript 可以在服务器端发出POST请求吗?

Javascript 可以在服务器端发出POST请求吗?,javascript,server,axios,graphql,middleware,Javascript,Server,Axios,Graphql,Middleware,我正在设计客户端和GraphQLAPI之间的中间件。其思想是创建一条路由,所有客户端请求首先到达/filline。然后,该路由决定是否可以从缓存发回数据,或者继续执行/graphql以访问那里的解析器以从数据库获取数据。我正在使用Axios发出请求,但它没有启动服务器端。我想问的是,这是否只是使用Axios的不当方式,还是我做得不对 Server.js const express = require('express'); const { graphqlHTTP } = require('exp

我正在设计客户端和GraphQLAPI之间的中间件。其思想是创建一条路由,所有客户端请求首先到达
/filline
。然后,该路由决定是否可以从缓存发回数据,或者继续执行
/graphql
以访问那里的解析器以从数据库获取数据。我正在使用Axios发出请求,但它没有启动服务器端。我想问的是,这是否只是使用Axios的不当方式,还是我做得不对

Server.js

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const fetch = require("node-fetch");
const redis = require("redis");

const PORT = process.env.PORT || 4000;
// const REDIS_PORT = process.env.REDIS_PORT || 6379;
//Create Redis client on Redis port (optional)
const client = redis.createClient();
const schema = require('./schema');
const bluebird = require('bluebird')
const app = express()

// bluebird.promisifyAll(redis.RedisClient.prototype);
// bluebird.promisifyAll(redis.Multi.prototype);
app.use(express.json())

client.on("error", err => {
  console.log("Error " + err);
});

client.on('connect', () => {
  console.log('Redis client connected');
});
// pass redis as context so our schema can use Redis methods

const wrapper = require('./filamentMiddleware')

app.use('/filament',
  wrapper(client), // filamentMiddleware with access to client
)

app.use(
  '/graphql',
  graphqlHTTP((req) => ({
    schema,
    graphiql: true,
    context: {
      client,
      req: req
    }
  })),
  // addToCacheWrapper(res.data)
);

app.listen(PORT, () =>
  console.log(`GraphQL server is running on port: ${PORT}`)
);

中间件

const axios = require('axios')
const serverFilamentQuery = require('./serverFilamentQuery')
const mergeDataFromCacheAndServer = require('./mergeDataFromCacheAndServer')

const wrapper = (client) => (req, res, next) => {
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  client.get(ip, (err, cacheData) => {
    const { query } = req.body
    // ip not found in cache
    if (!cacheData) {
      console.log('cacheData:', cacheData)
      axios.post('/graphql', { query }).then(res => {
        console.log('have we made the post ????')
        // set redis cache
        // set the new data in Redis
        client.set(ip, JSON.stringify({
          todos: res.data.data['todos']
        }), (err, result) => {
          console.log(result)
        })

        const { data } = res.data
        // return combinedData to client
        return res.status(200).json({ data })
      })
    }
    // console.log('ip found in cache')
    const [newQuery, data, isMatched] = serverFilamentQuery(query, cacheData)

    if (isMatched) {
      return res.status(200).json({ data })
    } else {
      axios.post('/graphql', { newquery }).then(res => {
        const merged = mergeDataFromCacheAndServer(data['todos'], res.data.data['todos']);
        // set the new data in Redis
        console.log(merged)
        client.set(ip, JSON.stringify({
          todos: merged
        }), (err, result) => {
          console.log(result)
        })
        // return combinedData to client
        return res.status(200).json({ merged })
      })
    }
  })
}


module.exports = wrapper

是的,使用Axios进行post请求服务器端没有问题

按照

浏览器和node.js(即服务器端)基于Promise的HTTP客户端

请注意,根据


与客户端电话相比

感谢您提供文档链接。看起来要在node.js中发出请求,需要使用完整的URI。我正在尝试将文章发送到
/graphql
,但不确定如何访问完整的URI。我喜欢任何指向正确方向的东西。难道你不能只使用你自己的主机服务器的URI(无论你的后端在哪里运行)并附加需要调用的GraphQL端点吗?