Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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/40.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访问文件的URL参数_Javascript_Node.js_Express - Fatal编程技术网

使用JavaScript访问文件的URL参数

使用JavaScript访问文件的URL参数,javascript,node.js,express,Javascript,Node.js,Express,在我的node.js项目中,我有一个server.js和一个文件夹“views”,其中有一个名为index.html的文件,所以现在当有人进入example.com(或example.com/index.html)时,他们会进入index.js,我正试图保护它,以便只允许通过特定的链接访问,例如 example.com->带您访问_denied.html 和 example.com/client={code-that-i-assign}exampleexample.com/client=th9w

在我的node.js项目中,我有一个server.js和一个文件夹“views”,其中有一个名为index.html的文件,所以现在当有人进入
example.com
(或
example.com/index.html
)时,他们会进入index.js,我正试图保护它,以便只允许通过特定的链接访问,例如

example.com
->带您访问_denied.html 和
example.com/client={code-that-i-assign}
example
example.com/client=th9wdn298n3d2

我试着用urlparms来做,但是我做不到,有人能帮我解决这个问题吗

函数api(){
const apikey=“”;
const searchselect=window.location.search;
const urlsearchparams=urlsearchparams(searchselect);
urlsearchparams.has(apikey).then(返回true);
}

我想了解一下,您想检查url中是否有get变量,比如enter和use determinate pages的标记,为此,您必须在server.js文件中为页面提供服务的中间件之前使用express中间件,如:

app.use(function (req, res, next) { // controlla il token
  const foo = req.query.foo;

  if (foo !== 'MAGIC_KEY' /* or what control you want to do */) next(Error('Nope'));
  else next();
});
app.use(express.static(path.join(__dirname, 'public')));
并调用带有此参数的页面
/?foo=MAGIC_KEY

用您的代码编辑:

const path = require("path");
const express = require("express");
const app = express();

// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// this is the code I writed before
app.use(function (req, res, next) { //  /?foo=MAGIC_KEY
  const foo = req.query.foo;

  if (foo !== 'MAGIC_KEY') next(Error('Nope'));
  else next();
});

// This is the middleware that serve the index
// https://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

app.use(express.static(path.join(__dirname + "/views/gg.txt")));

const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

我认为,为了理解,您需要检查url中是否有get变量,如enter和use determinate pages的令牌,为此,您必须在server.js文件中为页面提供服务的中间件之前使用express中间件,如:

app.use(function (req, res, next) { // controlla il token
  const foo = req.query.foo;

  if (foo !== 'MAGIC_KEY' /* or what control you want to do */) next(Error('Nope'));
  else next();
});
app.use(express.static(path.join(__dirname, 'public')));
并调用带有此参数的页面
/?foo=MAGIC_KEY

用您的代码编辑:

const path = require("path");
const express = require("express");
const app = express();

// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// this is the code I writed before
app.use(function (req, res, next) { //  /?foo=MAGIC_KEY
  const foo = req.query.foo;

  if (foo !== 'MAGIC_KEY') next(Error('Nope'));
  else next();
});

// This is the middleware that serve the index
// https://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

app.use(express.static(path.join(__dirname + "/views/gg.txt")));

const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

在这个特定的实例中,您可以逐个管理请求,但是这通常不是一个很好的解决方案。只需将
\key
替换为您需要的任何内容。如果您登陆
example.com
,它将以
access denied.html
响应,但是,例如,请求
example.com/key
将使您进入索引

const path = require("path");
const express = require("express");
const app = express();

// This is the middleware that serve the index
app.get("/", (request, response) => {
  response.sendFile(__dirname + "/public/access-denied.html");
});

app.get("/key", (request, response) => {
  response.sendFile(__dirname + "/public/index.html");
});

app.use(express.static(path.join(__dirname + "/public/gg.txt")));

const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

在这个特定的实例中,您可以逐个管理请求,但是这通常不是一个很好的解决方案。只需将
\key
替换为您需要的任何内容。如果您登陆
example.com
,它将以
access denied.html
响应,但是,例如,请求
example.com/key
将使您进入索引

const path = require("path");
const express = require("express");
const app = express();

// This is the middleware that serve the index
app.get("/", (request, response) => {
  response.sendFile(__dirname + "/public/access-denied.html");
});

app.get("/key", (request, response) => {
  response.sendFile(__dirname + "/public/index.html");
});

app.use(express.static(path.join(__dirname + "/public/gg.txt")));

const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

你能向我们展示你到目前为止拥有的吗?当然,你在服务器端使用的是express吗?是的@NickParsons你能向我们展示你到目前为止拥有的吗?当然,你在服务器端使用的是express吗?是的@NickParsons这不起作用这是我的代码-->我只需要在你的代码中使用index.js,您将中间件放在为索引页提供服务的中间件之后。我用editIt编辑答案仍然不起作用这里有一个GIF显示发生了什么,我在更改后用public更改了视图你重新启动服务器了吗?我尝试使用server.js和答案中的代码以及一个简单的views/index.html。当我调用
localhost:3000/?foo=MAGIC\u KEY
时,我会看到页面,在那里我没有使用
?foo=MAGIC\u KEY
或者我设置了一个错误的
foo
值,比如
MAGIC\u KEY
服务器会返回一个错误。这是代码嘿,这不起作用这是我的代码-->我只需要在代码中设置index.js,您将中间件放在为索引页提供服务的中间件之后。我用editIt编辑答案仍然不起作用这里有一个GIF显示发生了什么,我在更改后用public更改了视图你重新启动服务器了吗?我尝试使用server.js和答案中的代码以及一个简单的views/index.html。当我调用
localhost:3000/?foo=MAGIC\u KEY
时,我会看到页面,在那里我没有使用
?foo=MAGIC\u KEY
或者我设置了一个错误的
foo
值,比如
MAGIC\u KEY
服务器会返回一个错误。这就是代码