Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
How to accept a form from the client side to the server?

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) => { // событие "сообщение чата"
.....
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda