【发布时间】:2018-02-22 16:13:50
【问题描述】:
单击 public/index.html 中的按钮时,我被定向到 game.html,但它没有显示任何响应。它说This site can’t provide a secure connection: localhost sent an invalid response. 当我转到 localhost:3000 并单击按钮后,我被引导到 game.html,但服务器没有加载该页面。
public/index.html
<!-- HTML -->
<button class="w3-button w3-hover-black"> </button>
<!-- JavaScript -->
<script>
var buttons= document.getElementsByClassName('w3-button w3-hover-black');
for(i=0;i<buttons.length;i++){
buttons[i].onclick = function(){
window.location.href = "http://localhost:3000/game.html";
}
}
</script>
public/game.html
<div id = "gameDiv"></div>
<script src = "/socket.io/socket.io.js"></script>
<script src = "lib/phaser.min.js"></script>
<script src = "main.js"></script>
<script src = "player.js"></script>
服务器/server.js
var app = express();
const port = process.env.PORT ||3000 ;
const pathjoin = path.join(__dirname, '../public');
const pathjoin1 = path.join(__dirname,'../public/game.html');
var server = http.createServer(app);
app.use(express.static(pathjoin));
app.get('/game.html',(req,res)=>{
res.sendFile(pathjoin1);
});
var io = socketIo(server);
io.on('connection',(socket)=>{
console.log('user connected ' + socket.id);
});
【问题讨论】:
-
你是想在去/game的时候下载html文件,还是想渲染成html?
-
渲染成html
标签: javascript html node.js express