Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法获取/file.js_Javascript_Node.js_Nginx_Get_Server - Fatal编程技术网

Javascript 无法获取/file.js

Javascript 无法获取/file.js,javascript,node.js,nginx,get,server,Javascript,Node.js,Nginx,Get,Server,在过去的几天里,我一直在与nodejs合作,我一直在尝试以某种方式运行它。 这就是我所做的: 1.使用以下代码创建file.js: var express=require('express'); var-app=express(); var path=require('path'); var Chance=要求(“机会”); var chance=新机会(); app.get('/',函数(req,res){ if(chance.integer({min:1,max:10})==1){ res.

在过去的几天里,我一直在与nodejs合作,我一直在尝试以某种方式运行它。 这就是我所做的: 1.使用以下代码创建file.js:

var express=require('express');
var-app=express();
var path=require('path');
var Chance=要求(“机会”);
var chance=新机会();
app.get('/',函数(req,res){
if(chance.integer({min:1,max:10})==1){
res.sendFile(path.join(_dirname+'/file1.js');
}否则{
res.sendFile(path.join(_dirname+'/file2.js');
}
});
app.listen(80);
2.创建file1和file2 Java脚本(每个脚本都包含一个window.alert 3.使用如下配置启动nginx:

server {
listen 80;

server_name example.com;

location / {
    proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
app.get('/file.js', function(req, res) { ... });
}

但每当我转到Ip/file.js时,它都会说: 无法获取/file.js

显然我做了node file.js并启动了nginx!!!
为什么我会这样?!

您不是在
ip/file.js
上提供文件,而是在
ip/
上提供文件。您的路径定义如下:

app.get('/', function(req, res) { ... });
脚本的文件名与访问它的地址无关。要获得预期的结果,路由定义必须如下所示:

server {
listen 80;

server_name example.com;

location / {
    proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
app.get('/file.js', function(req, res) { ... });

你的nginx和node.js服务器都在端口80上侦听?嗯,是吗?错了吗?你不能在同一个端口上同时侦听这两个节点,这是不可能的。你可能没有运行你的节点脚本,或者你的nginx没有运行。我很确定nodejs正在运行。我不能对nginx说同样的话,尽管我在做这件事时没有遇到任何错误!哦,天哪……是的你说得对!!!我还输入了sendFIle它应该是sendFIle Thnx mate!这显然对我有很大帮助!没问题,很高兴我能帮上忙!如果这为你排序,你能将答案标记为已接受吗?只有一个问题。我能确保只有特定的域/IP才能从文件中得到响应吗?!@JMonreq.ip和
req.hostname
,然后将其与路由处理程序中允许的值列表进行比较-当然,这些值可能被某些人伪造,因此这是否足够安全取决于您使用它的目的。