Cannot POST express error

Asked

Viewed 894 times

-1

I’m trying to create a login system, follow my form:

<form action="/auth" class="login-form" method="POST">
  <h1>Login</h1>

  <div class="txtb">
    <input type="text" name="username" id="username" placeholder="Username">
  </div>

  <div class="txtb">
    <input type="password" name="password" id="password" placeholder="Password">
  </div>

  <input type="submit" class="logbtn" value="Login">

  <div class="bottom-text">
    Não ten uma conta? <a href="#">Sign up</a>
  </div>

</form>

My server.js

const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const mysql = require('mysql');
const session = require('express-session');
const bodyParser = require('body-parser');
const path = require("path");

const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'public'));
app.engine('html',require('ejs').renderFile);
app.set('view engine','html');

app.get('/', (req, res) => {
    res.render('index.html');
});

app.get('/chat', (req, res) => {
    res.render('chat.html');
});


let messages = [];

io.on('connection', socket => {
    console.log('Socket conectado');

    socket.on('sendMessage', data => {
        messages.push(data);
        socket.broadcast.emit('receivedMessage', data);
    });
});

//Login

var connection = mysql.createConnection({
    host:'localhost',
    user: 'root',
    password: '', 
    database: 'sanderson'
});

connection.connect(function(error) {
    if(!!error){
        console.log('Erro!')
    }
    else{
        console.log('Conectou :)')
    }
});

var log = express();
log.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: true
}));
log.use(bodyParser.urlencoded({extended : true}));
log.use(bodyParser.json());

log.get('/', function(request, response) {
    response.sendFile(path.join(__dirname + '/index.html'));
});

log.post('/auth', function(request, response) {
    var username = request.body.username;
    var password = request.body.password;
    if (username && password) {
        connection.query('SELECT * FROM user WHERE nome = ? AND senha = ?', [username, password], function(error, results, fields) {
            if (results.length > 0) {
                request.session.loggedin = true;
                request.session.username = username;
                response.redirect('public/chat.html');
            } else {
                response.send('Incorrect Username and/or Password!');
                response.redirect('public/test.html');
            }           
        });
    } else {
        response.send('Please enter Username and Password!');
        response.redirect('public/test.html');
    }
});

server.listen(3000);

My index.html is the form, when I click to send this error appears.

  • Does this code work? where you declared the variable server?

  • Yes it works, server.js is the file where I started the routes and express, but in my question I put only the part that corresponds to my login attempt

  • I edited and put the whole server.js to see if they can help me

  • posted with the editions

1 answer

0


In your html you must declare the tags name and placeholder within the tag input.

Modifying your html we have the following code:

<form action="/auth" class="login-form" method="POST">
    <h1>Login</h1>

    <div class="txtb">
      <input type="text" name="username" id="username" placeholder="Username">
    </div>

    <div class="txtb">
      <input type="password" name="password" id="password" placeholder="Password">
    </div>

    <input type="submit" class="logbtn" value="Login">

    <div class="bottom-text">
      Não ten uma conta? <a href="#">Sign up</a>
    </div>

  </form>

Note that the choice of the names of these variables is not random, these are the names that are declared in your module app.js, within the post function of the route /auth , vc uses the values of these variables to query the database. Hence the declaration of these names.

It will now be possible to use your connection to the bank to verify the user and password

The code I used on my express was the following:

app js.

const express = require('express')
const path = require('path')
var log = express();

log.use(express.urlencoded({extended : true}));
log.use(express.json());

log.get('/', function(request, response) {
    response.sendFile(path.join(__dirname + '/public/index.html'));
});

log.post('/auth', function(request, response) {
    console.log(request.body)
    var username = request.body.username;
    var password = request.body.password;
    if (username && password) {
        connection.query('SELECT * FROM user WHERE nome = ? AND senha = ?', [username, password], function(error, results, fields) {
            if (results.length > 0) {
                request.session.loggedin = true;
                request.session.username = username;
                response.redirect('public/chat.html');
            } else {
                response.send('Incorrect Username and/or Password!');
                response.redirect('public/test.html');
            }           
        });
    } else {
        response.send('Please enter Username and Password!');
        response.redirect('public/test.html');
    }
});

log.listen(3000);
  • thank you very much, but I believe that the error is in the server.js file, because I had already defined the name tag, as you pointed out just did not put the id, and the placeholder that in my conception is just something visual. I made the changes in html but unfortunately the error persists.

  • Yes, I already made the change, and I switched span for input, but still the same mistake.

  • as far as I have tested, the connection to db works

  • I discovered my error, I declared two variables = express(); the app variable and the log, I replaced the log by the app the variable I used at the beginning of the server.js file, and it worked, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.