-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
?– Danizavtz
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
– Sanderson Milagres
I edited and put the whole server.js to see if they can help me
– Sanderson Milagres
posted with the editions
– Sanderson Milagres