繁体   English   中英

加载随机 html 页面 Express.js

[英]Load random html page Express.js

Nimit 这边,我的代码需要一些帮助。 所以我可以在同一个 url 上加载不同的 html 页面,例如:www.xyz.com/home/random 可以随机超过 1 个 html 页面

task1.js 模拟。 我只有在 html 文件名发生变化的情况下才有 task2.js、task3.js、task4.js。

//This is the root path!
const path = require("path");
const rootDir = require("../utils/path");

//express routing
const express = require("express");
const router = express.Router();

router.use("/random", (req, res, next) => {
  res.sendFile(path.join(rootDir, "views", "task_1.html"));
});

module.exports = router;

应用程序.js

const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");

const app = express();

const homeRouter = require("./routes/homeRouter");
const taskRouter1 = require("./routes/task1");
const taskRouter2 = require("./routes/task2");
const taskRouter3 = require("./routes/task3");
const taskRouter4 = require("./routes/task4");
const errorRouter = require("./routes/error");

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));

app.use(homeRouter);

// var ch = 1;
// switch (ch) {
//   case 1:
//     app.use(taskRouter.router1);
//     break;
//   case 2:
//     app.use(taskRouter);
//     break;
//   case 3:
//     app.use(taskRouter);
//     break;
//   case 4:
//     app.use(taskRouter);
//     break;
// }
task = Math.floor(Math.random() * 4);
console.log(task);
switch (task) {
  case 0:
    app.use(taskRouter1);
    break;
  case 1:
    app.use(taskRouter3);
    break;
  case 2:
    app.use(taskRouter3);
    break;
  case 3:
    app.use(taskRouter4);
    break;
}
app.use(errorRouter);
console.log("it has started!");

app.listen(1000);

所以事情是它只随机加载一个页面,刷新后它也会加载相同的页面。

你也许可以使用这样的东西:

router.use("/random", (req, res, next) => {
  const task = Math.floor(Math.random() * 4);
  res.sendFile(path.join(rootDir, "views", `task_${task}.html`));
});

首先,将您的 URL 加载到数组中

const arrPaths = []
arrPaths[0] = "/public/task1.html"
arrPaths[1] = "/public/task2.html"

或者您可以使用文件系统搜索来填充它。这取决于您,但您可以执行此操作。 只需填充您将从中随机选择的 arrPaths

然后我们设置快速路由器以在调用 GET /random 时做出响应

router.get("/random", (req, res) => {

现在我们需要随机选择一条路径

var chosenPath = arrPaths[Math.floor(Math.random() * (arrPaths.length))];  

生成 0 到 arrPaths.length-1 并抓取对应的 arrPaths 元素

然后我们发送路径作为响应。

  res.sendFile(path.join(__dirname + chosenPath));
})

你的没有用,因为你只做随机一次。 为此,您需要在每次调用请求时生成随机元素。

主页.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MAIN</title>
  </head>
  <body>
    <a href="/random" method="GET">xyz</a>
  </body>
</html>

我做了一个设置,你也应该使用它。 创建一个新文件夹 utils,创建一个名为 path.js 的文件(这为您提供了根目录的路径。) path.js

const path = require("path");

module.exports = path.dirname(process.mainModule.filename);

所以这个问题有两个替代答案,我会提到两个。

1. task.js(在express中用作路由器)

//This is the root path!
const path = require("path");
const rootDir = require("../utils/path");

//express routing
const express = require("express");
const router = express.Router();

// router.use("/random", (req, res, next) => {
//   res.sendFile(path.join(rootDir, "views", "task.html"));
// });

router.use("/random", (req, res, next) => {
  const task = Math.floor(Math.random() * 4);
  console.log(task);
  res.sendFile(path.join(rootDir, "views", `task_${task}.html`));
});

module.exports = router;
  1. taskie.js

    常量 arrPaths = []; arrPaths[0] = "/views/task_0.html"; arrPaths[1] = "/views/task_1.html";

    常量路径 = 要求(“路径”); const rootDir = require("../utils/path");

    常量表达 = 要求(“表达”); 常量路由器 = express.Router();

    router.get("/random", (req, res) => { var selectedPath = arrPaths[Math.floor(Math.random() * arrPaths.length)]; console.log(chosenPath); res.sendFile(path .join(rootDir + selectedPath)); });

    module.exports = 路由器;

还有第三种选择,我不建议申请。 如果你想知道,请ping我!

暂无
暂无

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

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