我在NodeJS上使用socket.io mysql和html。我想登录另一个html页面,而不是我的index.html(如login.html)。
通过大量搜索我做到了这一点,但是当我点击链接时,内容始终是index.html的内容
app.js
const http = require('http');
const fs = require('fs');
const mysql = require('mysql');
const path = require('path');
const ext = /[\w\d_-]+\.[\w\d]+$/;
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "database"
});
const server = http.createServer(function(req, res){
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('views/index.html').pipe(res);
} else if (ext.test(req.url)) {
console.log(req.url);
fs.exists(path.join(__dirname, req.url), function (exists) {
if (exists) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('views/index.html').pipe(res);
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
fs.createReadStream('views/404.html').pipe(res);
}
});
}
});
const io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
});
server.listen(8080);
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<a href="views/login.html">Login</a>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
</script>
</body>
</html>
度过愉快的一天,