简体   繁体   English

无法获取/file.js

[英]Cannot GET /file.js

I've been working with nodejs for the past days and i'm stuck on trying to run it somehow. 在过去的几天里,我一直在使用nodejs,但我一直试图以某种方式运行它。 This is what i did: 1.Create a file.js with this code: 这就是我所做的:1.使用以下代码创建file.js:

var express = require('express');
var app = express();
var path = require('path');
var Chance = require('chance');

var chance = new Chance();

app.get('/', function(req, res) {
    if (chance.integer({min: 1, max: 10}) == 1) {
        res.sendFile(path.join(__dirname + '/file1.js'));
    } else {
        res.sendFile(path.join(__dirname + '/file2.js'));
    }
});

app.listen(80);

2.Created the file1 and file2 javascripts(Each contain a window.alert 3.Started nginx with the config like this: 2.创建了file1和file2 javascripts(每个都包含一个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;
}

} }

But whenever i go to Ip/file.js it says: Cannot GET /file.js 但是,每当我转到ip / file.js时,它都会说:无法获取/file.js

Obviously i did node file.js and started nginx!!! 显然我做了节点file.js并启动了Nginx !!! Why am i getting that?! 我为什么得到那个?

You're not serving the file at ip/file.js , you're serving it at ip/ . 您不在ip/file.js提供文件,而是在ip/ Your route is defined here: 您的路线在这里定义:

app.get('/', function(req, res) { ... });

The filename of the script has no bearing on what address you access it from. 脚本的文件名与您从其访问的地址无关。 To get the result you're expecting, the route definition would have to look something like this: 为了获得您期望的结果,路由定义必须看起来像这样:

app.get('/file.js', function(req, res) { ... });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM