I am trying to create a test signup page. I have been able to get everything working and it appears that the server is properly connecting.
Here is my code:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware
app.use(bodyParser.json());
var Connection = require('tedious').Connection;
var config = {
server: 'XXXXXXXXXXXXXXXXXX', //update me
authentication: {
type: 'default',
options: {
trustServerCertificate: true
}
},
options: {
encrypt: false,
database: 'XXXXXXX', //update me
port: XXXXXXX
}
};
var connection = new Connection(config);
connection.connect();
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
ExecuteFunction();
});
connection.on('debug', function(err) { console.log('debug:', err);});
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
app.get('/signup', (req, res) => {
res.sendFile("XXXXXXXXXXXXXXXXXXXXX");
});
function ExecuteFunction() {
// Route to handle signup request
app.post('/signup', (req, res) => {
const { username, email, password } = req.body;
var request = new Request('INSERT INTO Users (Username, Email, PasswordHash) VALUES (@username, @email, @password)', function(err) {
if (err) {
console.log("Error:", err);}
});
request.addParameter('Username', TYPES.VarChar, username);
request.addParameter('Email', TYPES.NVarChar, email);
request.addParameter('PasswordHash', TYPES.VarChar, password);
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log(" New Signup " + column.value);
}
});
});
request.on("requestCompleted", function (rowCount, more) {
});
connection.execSql(request);
});
}
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
The file is supposed to create the local node server and connect to SQL Server. Then it should wait for requests from a webpage with signup. Once it receives a request, it will send it to SQL Server.
The current issue is that it connects and creates the local node server, but immediately goes to a final state and does not allow for new requests. The SQL Server config might be wrong with the SentLogin7WithStandardLogin
, but I have not been able to find any resources on any of this.
I am new to SQL Server / nodejs and any help would be appreciated!
Whenever I try and run this using connection debugging, I get these messages:
Server running on port 3000
debug: connected to XXXXXXXXXXXXXXXX
debug: State change: Connecting -> SentPrelogin
debug: State change: SentPrelogin -> SentLogin7WithStandardLogin
Connected
debug: State change: SentLogin7WithStandardLogin -> Final
debug: connection to XXXXXXXXXXXXXXXX closed
debug: State is already Final