Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!