You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复CORS POST请求中的Error: options.uri is a required argument错误?

Fixing "Error: options.uri is a required argument" in Your CORS POST Request

Hey there! Let's break down the error you're hitting and get your CORS POST request working properly.

What's Causing the Error?

The Error: options.uri is a required argument comes directly from the request library you're using—it needs to know which URL to send the request to, and you haven't provided that mandatory uri (or url, they're interchangeable) parameter when calling the request() function.

Issues in Your Current Code

Looking at your snippet, I spot a couple of key problems:

  • Missing request() call with target URL: You extracted url = req.url.substr(1), but haven't used this variable in a request invocation yet. That's exactly why the library is throwing the missing uri error.
  • Incorrect header property: You used req.header (singular) when setting headers, but the correct property is req.headers (plural). Also, overwriting req.headers = {} completely wipes out original request headers—this might break CORS handling later, so it's better to modify specific headers instead of resetting the whole object.
  • Deprecated library heads-up: The request library is no longer maintained. For long-term stability, consider switching to active alternatives like axios or node-fetch.

Fixed Code Example (Using request)

Here's how to adjust your middleware to properly forward the POST request and fix the error:

var express = require('express');
var request = require('request');
var cors = require('cors'); // Install with `npm install cors`
var app = express();

// Enable CORS to handle cross-origin requests automatically
app.use(cors());
// Parse JSON request bodies (required for POST data)
app.use(express.json());

app.use('/', function(req, res, next) {
  var targetUrl = req.url.substr(1); // Extract the target URL from the request path

  // Validate we have a target URL first
  if (!targetUrl) {
    return res.status(400).send('Target URL is required');
  }

  // Configure and send the forwarded request
  request({
    uri: targetUrl, // This is the mandatory parameter you were missing
    method: req.method, // Forward the original request method (POST)
    headers: {
      'Host': 'your-target-host.com', // Replace with your actual target host
      'Cache-Control': 'no-cache',
      'User-Agent': 'iPad',
      'Accept': '*/*',
      // Add any other required headers here
    },
    body: req.body // Forward the original POST request body
  }, function(error, response, body) {
    if (error) {
      console.error('Request forwarding failed:', error);
      return res.status(500).send('Failed to process your request');
    }
    // Send the target server's response back to the client
    res.send(body);
  });
});

app.listen(3000, function() {
  console.log('Server running on port 3000');
});

Since request is deprecated, here's a cleaner, more modern version using axios:

var express = require('express');
var axios = require('axios'); // Install with `npm install axios`
var cors = require('cors');
var app = express();

app.use(cors());
app.use(express.json());

app.use('/', async function(req, res, next) {
  var targetUrl = req.url.substr(1);

  if (!targetUrl) {
    return res.status(400).send('Target URL is required');
  }

  try {
    const response = await axios({
      url: targetUrl,
      method: req.method,
      headers: {
        'Host': 'your-target-host.com',
        'Cache-Control': 'no-cache',
        'User-Agent': 'iPad',
        'Accept': '*/*',
      },
      data: req.body
    });
    res.send(response.data);
  } catch (error) {
    console.error('Axios request error:', error);
    res.status(500).send('Failed to process your request');
  }
});

app.listen(3000, function() {
  console.log('Server running on port 3000');
});

Key CORS Tips

  • Always use the cors middleware to handle cross-origin headers automatically—it avoids manual setup mistakes.
  • Ensure your target server allows requests from your Express server's origin (use wildcard * for testing purposes only).

内容的提问来源于stack exchange,提问作者Yahya Sn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:34:26