I am creating a chat (on nodeJS), I wanted to send data from the form to the server from the registration page, so that the next page (the chat itself) displays the username. I was able to do this through localStorage, but as soon as I started connecting the database, I realized that I couldn’t do without sending it to the server. I was able to do this, but it only passes the value to app.post (that is, the scope is only this method call). I need to get it into a global variable in order to perform checks and send it to the client side. And how to make it so that when the send button is opened, another html page (the chat itself) opens.
index.html
<body>
<a href = "client/chat.html">Тык</a>
<form action="/index" method="post" class="FORM">
<label>Username</label><br>
<input type="TEXT" class="CHECK" autocomplete="off" name="Username"/>
<button type="submit" onclick="redirect()" class="BTN">Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src = "client/register.js"></script>
</body>
index.js(server)
const express = require('express');
const app = express();
const db = require('./database/database'); // подключаемся к бд
const bodyParser = require('body-parser');
const http = require('http');
const server = http.createServer(app);
const {Server} = require('socket.io');
const io = new Server(server);
const port = 3000;
let id = 0;
let users = {};
const urlencodedParser = bodyParser.urlencoded({
extended: false,
})
app.use(express.static(__dirname));
app.get('/index', (request, response ) => {
response.sendFile(__dirname + '/index.html');
});
app.post('/index', urlencodedParser, function (request,response) {
if (!request.body) return response.sendStatus(400)
console.log(request.body.Username) // мне надо чтобы значение Username было в глобальное переменной
})
io.on('connection', (socket) => {
socket.on('chat message', (msg) => { // событие "сообщение чата"
.....