如何修复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 extractedurl = req.url.substr(1), but haven't used this variable in arequestinvocation yet. That's exactly why the library is throwing the missingurierror. - Incorrect header property: You used
req.header(singular) when setting headers, but the correct property isreq.headers(plural). Also, overwritingreq.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
requestlibrary is no longer maintained. For long-term stability, consider switching to active alternatives likeaxiosornode-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'); });
Alternative: Using axios (Recommended)
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
corsmiddleware 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
相关产品推荐
相关产品推荐

