您如何在Node.js Web服务器上使用超链接?
我正在尝试向用户发送不同的html文件,其中index.html是一堆用户可以单击以查看该文件的超链接,但是我收到一条错误消息,提示“无法获取(该文件的名称)”每当我单击超链接时。
var app = require('express')();
var http = require('http').createServer(app);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
这是我的index.html:
<html>
<a href="/pongClient.html">Pong Game </a>
</html>
回答如下:您可以定义一个公共静态文件夹,如下所示:
app.use(express.static(__dirname + '/public'));
您公用文件夹中的所有内容都可以在URL的根目录(即/pongClient.html
)进行访问