Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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/7/user-interface/2.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
Node.js Nodejs Express重定向到可共享的唯一url,客户端JS工作_Node.js_Express - Fatal编程技术网

Node.js Nodejs Express重定向到可共享的唯一url,客户端JS工作

Node.js Nodejs Express重定向到可共享的唯一url,客户端JS工作,node.js,express,Node.js,Express,我正在节点中使用Express,我想执行以下操作: 如果用户来到页面(hi)或从未存在过的新url(hi/wxyz4321) 重定向到同一页面,但具有唯一的url,可以共享给其他人(hi/abcd1234) “abcd1234”被追加到一个名为URL的数组中 代码如下: const express = require('express'); const app = express(); const http = require('http').Server(app); const nanoi

我正在节点中使用Express,我想执行以下操作:

如果用户来到页面(hi)或从未存在过的新url(hi/wxyz4321)

  • 重定向到同一页面,但具有唯一的url,可以共享给其他人(hi/abcd1234)
  • “abcd1234”被追加到一个名为URL的数组中
代码如下:

const express = require('express');
const app = express();
const http = require('http').Server(app);
const nanoid = require('nanoid');
const fs = require('file-system');
var URLS = [];
var ID = nanoid();
app.use(express.static(__dirname + "/public"))
app.use("/styles",  express.static(__dirname + '/public/css'));
app.use("/scripts", express.static(__dirname + '/public/js'));
app.get('/',function(req,res){
    res.sendFile(path.join(__dirname+'/public/index.html'));
});

app.get("/*", function(req, res){
    var origin= req.url.slice(-21);
    //-21 because that is the length of nanoid generated
    if(URLS.includes(origin)===false){
        URLS.push(ID);
        fs.copyFileSync('public/index.html', "public/"+randomID+".html");
        //Creates a new html file with the name of ID
        //But res.redirect(__dirname + "/public/randomID"); does not work
    }
});

提前谢谢。

好的,我可能已经找到了答案。我的重定向工作正常,但Chrome阻止我加载本地资源,所以我需要将其部署到服务器上才能100%确定。不过,它确实将我重定向到了谷歌和其他现有网站

const express = require('express');
const app = express();
const http = require('http').Server(app);
const nanoid = require('nanoid');
const fs = require('file-system');
var URLS = [];
var ID = nanoid();
//app.use(express.static(__dirname + "/public"))
//I removed this line, because redirect will not work if user comes to index.html
app.use("/styles",  express.static(__dirname + '/public/css'));
app.use("/scripts", express.static(__dirname + '/public/js'));
//Retaining these two lines, because these lines where the css and js are kept

app.get("/*", function(req, res){
    var origin= req.url.slice(-21);
    //-21 because that is the length of nanoid generated
    if(URLS.includes(origin)===false){
        URLS.push(ID);
        fs.copyFileSync('public/index.html', "public/"+randomID+".html");
        //Creates a new html file with the name of ID
        var destination = '<script>window.location.href=' + '"' + __dirname + "/public/"+ randomID + '";</script>';
        //var destination = '<script>window.location.href=' + '"' +"https://www.google.com.my/imghp?hl=en&tab=wi&ogbl"+ '";</script>';
        //Since this line worked, it will probably work if I test this on a real server
        res.send(destination);
        //Redirects to the newly created html file
    }
});
const express=require('express');
常量app=express();
const http=require('http')。服务器(应用程序);
const nanoid=require('nanoid');
const fs=require(‘文件系统’);
var url=[];
var ID=nanoid();
//app.use(express.static(\uu dirname+“/public”))
//我删除了这一行,因为如果用户访问index.html,重定向将不起作用
app.use(“/styles”,express.static(uu dirname+'/public/css');
app.use(“/scripts”,express.static(_dirname+'/public/js');
//保留这两行,因为这两行保存了css和js
应用程序获取(“/*”,函数(请求,恢复){
var origin=req.url.slice(-21);
//-21因为这是生成的纳米线的长度
if(url.includes(origin)==false){
url.push(ID);
copyFileSync('public/index.html',“public/”+randomID+“.html”);
//创建一个名为ID的新html文件
var destination='window.location.href='+'''+uu dirname+'/public/'+randomID+';';
//var destination='window.location.href='+'''''+'https://www.google.com.my/imghp?hl=en&tab=wi&ogbl"+ '";';
//由于这行代码有效,如果我在真正的服务器上测试它,它可能会有效
res.send(目的地);
//重定向到新创建的html文件
}
});

res.redirect
?在我可以重定向之前,我需要先创建一个新的URL,对吗?您的描述中有一个重定向循环。如果有人带着
hi/abcd1234
进来,你不能将他们重定向到
hi/abcd1234
,因为这样他们会再次带着
hi/abcd1234
进来。等等,你的意思是第三个条件不是必须的,对吗?是的,谢谢你提醒我!对的您的逻辑应该是“如果
id
未知(“未定义”只是未知的特例,无需改变),创建新id并重定向到该id。“没有其他”。