简体   繁体   English

如果我在本地运行Node.js服务器,我究竟应该在http请求中使用什么作为参数'url'?

[英]What exactly should I use as an argument 'url' in http request if i run Node.js server locally?

I'm trying to make a simple http request to my local server (running by Node.js). 我正在尝试向我的本地服务器发送一个简单的http请求(由Node.js运行)。 How my needed url should looks like? 我需要的网址应该如何?

I'm trying to make my website behaves in a next way: After button click the script is run. 我正试图让我的网站以下一种方式运行:点击按钮后运行脚本。 Script forms http request and sends it to my Node server, which I run locally on my computer with command line. 脚本形成http请求并将其发送到我的节点服务器,我使用命令行在我的计算机上本地运行。 The Node server handles http request, fetches data from my mongodb database and responds with this data to my script. 节点服务器处理http请求,从我的mongodb数据库中获取数据,并将此数据响应到我的脚本。 Finally, the script must output the data as a content on a webpage. 最后,脚本必须将数据作为内容输出到网页上。 The problem is that I get this error in a browser console: 问题是我在浏览器控制台中收到此错误:

'Access to XMLHttpRequest at 'file:///C:/.../serverSide.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.' '对来自'null'的'file:/// C:/.../ serverSide.js'访问XMLHttpRequest已被CORS策略阻止:仅对协议方案支持跨源请求:http,data,chrome, chrome-extension,https。'

I guess it is because of invalid url, which I used as an argument in my http request. 我想这是因为无效的url,我在http请求中用作参数。 All of my files (html, script and node serverside) are in the same folder. 我的所有文件(html,脚本和节点服务器端)都在同一个文件夹中。 I have already used following url values: 'serverSide.js' , 'http://localhost:8080/' , 'localhost:8080' and all of them caused an error which is above. 我已经使用了以下url值: 'serverSide.js''http://localhost:8080/''localhost:8080' ,所有这些都导致了错误。 So, what should I use as url ? 那么,我应该使用什么作为url

my script: 我的剧本:

function textOutput() {

    let xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function () {
        if ( this.readyState == 4 && this.status == 200 ) {
            document.getElementById('questionText').innerHTML = 
this.responseText
        }
    };

    xhttp.open('GET', 'serverSide.js', true);
    xhttp.send();
};

here is part of my serverside which is handling the request: 这是我的服务器端处理请求的一部分:

...
http.createServer(function (req, res) {
    ukrlitquestions.findOne({id: randomId(1, docsAmount)}, 'text', 
function(err, result) {
        if (err) console.log(err);
        let quesText = result.text;
        res.write(quesText);
        console.log(quesText);
        res.end();
    });
}).listen(8080);
...

You'll need to point to the correct domain. 您需要指向正确的域。 Something like http://localhost:3000/server-side.js http://localhost:3000/server-side.js

What you are seeing is a CORS error. 你看到的是一个CORS错误。 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

The web browser sends an OPTION request before sending the real request. Web浏览器在发送实际请求之前发送OPTION请求。 The Url should be something like http://localhost:8080/server-side.js Url应该类似于http://localhost:8080/server-side.js

Try adding the following to the server side: 尝试将以下内容添加到服务器端:

 if (req.method === "OPTIONS") {
    res.sendStatus(200);
  }

打开http.open('GET',' http:// localhost:8080 / serverSide.js ',true)

You need to allow CORS from your server. 您需要从服务器允许CORS You can achieve that by adding this. 你可以通过添加它来实现这一点。

...
http.createServer(function (req, res) {
  res.setHeader('Access-Control-Allow-Origin, '*')
  ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我应该在带有 node.js 的 http 服务器中的请求处理对象上声明 handle["/"] ? - why should i declare handle["/"] at request handling object in http server with node.js? 是否应将Babel用于Node.js服务器应用程序? 为什么呢? - Should I use Babel for a Node.js server app or not? and why? 我应该使用什么编码来在 node.js 中“gzuncompress()”? - What's the encoding should I use to “gzuncompress()” in node.js? 当我想使用Ajax通过node.js发送/接收请求时,如何在xmlhttp.open()中设置参数“ url” - How to set the argument “url” in xmlhttp.open() when I want to use Ajax to send/receive request with node.js 如何离线/本地使用Node.js? - How can I use Node.js Offline/locally? Node.js request.open(“GET”, “what.should.I.put.here.html/getAll”, true); - Node.js request.open(“GET”, “what.should.I.put.here.html/getAll”, true); 我可以卷曲到node.js服务器,但无法从javascript发出http请求 - I am able to curl to node.js server but unable to http request from javascript 如何执行对由node.js服务器处理的http请求的查询字符串中的特定参数序列的检查? - How do I enforce a check for a particular sequence of parameters in the querystring of an http request handled by a node.js server? 如何通过Node.JS Server响应非标准(非HTTP)请求? - How can I respond back to a non standard(non HTTP) request over Node.JS Server? 如何转移这个 http Node.Js 在 https 上运行? - How Can I Transfer This http Node.Js to run on https?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM