Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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/3/html/79.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/scala/19.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 当使用node js单击hyperlink href时,如何重定向到另一个页面?_Javascript_Html_Node.js - Fatal编程技术网

Javascript 当使用node js单击hyperlink href时,如何重定向到另一个页面?

Javascript 当使用node js单击hyperlink href时,如何重定向到另一个页面?,javascript,html,node.js,Javascript,Html,Node.js,server.js: var http = require('http'); var fs = require('fs'); function onRequest(request,response){ response.writeHead(200, {'content-Type':'text/html'}); fs.readFile('./index.html',null,function(error,data){ if(error) {

server.js:

var http = require('http');
var fs = require('fs');

function onRequest(request,response){
    response.writeHead(200, {'content-Type':'text/html'});
    fs.readFile('./index.html',null,function(error,data){
        if(error) 
        {
            response.writeHead(404);
            response.write('File not found');
        }
        else
        {
            response.write(data);
        }
        response.end();
    });
    fs.readFile('./about.html',null,function(error,data){
        if(error) 
        {
            response.writeHead(404);
            response.write('File not found');
        }
        else
        {
            response.write(data);
        }
        response.end();
    });
}

http.createServer(onRequest).listen(8080);
我是Node.js的新手,在这里我在文件夹中创建了一个简单的HTML页面,即
index.HTML
about.HTML
。我还创建了一个
server.js

现在,当我在
cmd
上运行命令并在
localhost:8080
上运行时,会显示index.html页面,但当我单击超链接,即
时,它不会工作


那么,如何在node js中创建超链接呢

要在express中提供静态文件,您需要使用express.static配置中间件,如下所示:

app.use('/', express.static('html'));
这样,如果您的所有html文件都在html文件夹中,express会将/映射到html文件所在的位置


请记住,路径“/”是相对于您开始节点进程的位置。

要呈现不同的html文件,必须使用基于url的重定向。我已经用你的例子把它说得更清楚了

index.html
您尝试重定向时使用的代码可能重复?见