gpt4 book ai didi

javascript - Express 不执行任何操作,只发送 HTML 文件

转载 作者:行者123 更新时间:2023-12-01 00:57:50 27 4
gpt4 key购买 nike

我在 Express 中使用 app.post 方法,如下所示:

app.post('/race', function(req,res) {
let raceResponse = {
user_name: req.body.userName
}
console.log('Express has received race.');

//Socket.IO
let race = io.of('/race').on('connection', (socket) => {
console.log('A user has entered the race!');
socket.on('racePageReady', function() {
console.log('race page ready recieved');
socket.emit('racePageInfo', raceResponse);
});

socket.on('createRoom', function(roomName) {
socket.join(roomName);
let clients = io.sockets.adapter.rooms[roomName].sockets;
console.log("A room with the name " + roomName + "was created.");
console.log(clients);
socket.emit('roomCreated', clients);
});

socket.on('joinRoom', function(roomName) {
socket.join(roomName);
let clients = io.sockets.adapter.rooms[roomName].sockets;
console.log('A user joined the room with the name: ' + roomName + ". The user's name is " + raceResponse.user_name);
console.log(clients);
socket.emit('roomCreated', clients);
});
});

res.sendFile(path.join(__dirname, '/client/race/index.html'));
}

页面发送正常,但 console.log 和所有其他 Socket.IO 内容却没有发生。我发现这很奇怪,因为我有一个不同 app.post 方法,工作得很好,所有 console.logging 和 Socket.IO生意发生了。代码如下:

app.post('/city', function(req,res) {
let cityResponse = {
user_name: req.body.userName
}

console.log('Express has received city.');

//Socket.IO
let city = io.of('/city').on('connection', (socket) => {
socket.id = Math.random();
socket.name = cityResponse.user_name;
SOCKET_LIST[socket.id] = socket; //defined earlier

User.onConnect(socket); //defined earlier

socket.on('cityPageReady', function() {
socket.emit('cityPageInfo', cityResponse);
console.log('city page ready recieved');
});

console.log('A user has connected to the city!');
console.log("Socket: " + socket);
console.log("Socket ID: " + socket.id);
console.log("SOCKET_LIST: ");
console.log(SOCKET_LIST);

socket.on('chat message', function(msg, user) {
console.log('User ' + user + ' sent the message : ' + msg);
socket.emit('chat message', msg, user);
});
});

res.sendFile(path.join(__dirname, '/client/city/index.html'));
});

据我所知,这两种方法看起来几乎相同,除了中间的 Socket.IO 内容。我相当确定 io.of 方法是正确的,因为它适用于城市页面,但不适用于比赛。

唯一的区别是这两个页面的访问方式。城市页面是通过带有 action 属性的 HTML 表单访问的,而竞赛页面是通过带有 href 属性的 HTML 链接(在城市页面上)访问的。

两种方法如下所示:

城市

<form id="cityForm" action="http://localhost:4000/city" method="POST">
User name: <input type="text" name="userName">
button type="submit" id="formSubmit">Submit</button>
</form>

种族

<div><a href="http://localhost:4000/race"></div>

有人能明白为什么会发生这种奇怪的行为吗?如果需要任何其他信息,请告诉我,以便我将其包含在内。

最佳答案

当点击 HTML 链接时,浏览器会发出 GET HTML 请求。当您提交表单(使用 method="POST")时,浏览器会执行 POST 请求。

当使用app.post()时,你告诉express监听POST请求。如果你想express监听GET请求,你应该使用app.get()

关于javascript - Express 不执行任何操作,只发送 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384582/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com