Issues with output while connecting using nodejs mssql - sql

I'm new to node.js and I'm trying to connect to a intersystems-cache database. Here is what I have, based off the sample code in the docs:
var sql = require('mssql');
console.log("Connecting...");
sql.connect("mssql://username:password#server:1234/DB").then(function(){
console.log("connected");
}).catch(function(err) {
console.log(err)
});
If I put in the wrong server or port, I get a Failed to connect error, but no matter what else I enter incorrectly (username, pass, db) I get zero output. More importantly, when all the data is correct I never get the connected output.
Is this a compatibility issue with Cache and the mssql library? Or am I doing something wrong?

why do you use mssql, instead of cache, while you have to connect to cache.
If you look at the documentation, you may find and example
var globals = require('cache');
var mydata = new globals.Cache();
mydata.open(
{ path:"/cache20102/mgr",
username: "_SYSTEM",
password: "SYS",
namespace: "USER"
},
function(error, result){}
);

Related

mssql in nodejs not connecting

Trying to connect to my database to create a local website but having connection issues.
Running in node.js, this is my server.js file:
let sql = require('mssql');
let config = {
user: 'sa',
password: 'password',
server: '10.0.1.130\\SQLSERVER',
database: 'MY_DB',
};
function connect() {
let dbConn = new sql.ConnectionPool(config);
dbConn.connect()
}
connect();
Upon running this I get the following error:
ConnectionError: Failed to connect to 10.0.1.130:undefined - self signed certificate
code: 'ESOCKET'
Not sure why it is removing \SQLSERVER but that seems to be my issue.
I know the credentials are all correct as I connect to this on another computer (ubuntu) but its being removed and we want to move it to windows, have not been able to connect to this point.
Any suggestions are appreciated.

Trying to connect to on-prem SQL Server using ntlm and nodejs

looking for an example or code that allows to connect a very simple node.js console application to a SQL Server Instance (2019) using ntlm and no name and password, using tedious and/or mssql.
For tedious, the code below works fine:
but I cannot seem to get the code to use ntlm without providing a name and password.
//
var Connection = require('tedious').Connection;
var config = {
server: '(local)',
authentication: {
type: 'default',
options: {
userName: 'sa',
password: 'your_sa_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'master',
trustServerCertificate: true
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
connection.connect();
//
(I have tried the above with and without the trustServerCertificate, it seems necessary)
For mssql, I am using the first example on this page:
https://tediousjs.github.io/node-mssql/#connect-callback
And it returns nothing - nothing at all, whether I use sql auth or ntlm. Not sure what is going on there - I have edited the catch block to have an error output, nothing happens.
The Instance is up, the code above runs, I have nothing special or unusual about the SQL Server that I know of at all.
Thoughts? Pointers? Any help deeply appreciated. I

electron certificates network

I am trying to write a simple electron app to interface with a REST server. The server doesn't have the appropriate certificates. When I try to make a 'GET' request (using fetch()), I get the following error message:
Failed to load resource: net::ERR_BAD_SSL_CLIENT_AUTH_CERT
Fixing the certs is not currently an option. I tried to use the 'ignore-certificates-error' flag (see below). It seems like it should allow me to skip over this error, but it doesn't.
var electron = require('electron');
var app = electron.app
app.commandLine.appendSwitch('ignore-certificate-errors');
...
The result is the same error.
Questions:
I am correct in assuming this options is supposed to help here?
If so, any ideas what I am doing wrong?
Electron version: 1.2.8
Thanks!
You can update your version of electron and use this callback:
app.on('certificate-error', (event, webContents, link, error, certificate, callback) => {
if ('yourURL/api/'.indexOf(link) !== -1) {
// Verification logic.
event.preventDefault();
callback(true);
} else {
callback(false);
}
});
That you going do the fetch to your api with https.

updating req.session outside of express

I'm using redisStore in express.
User session data is available at something like sess:eI6Jnj0gzMkdEvszUciqbcJ9, if i do:-
redisClient.set('sess:eI6Jnj0gzMkdEvszUciqbcJ9', 'something', function(err){
console.log(err || ' session changed');
});
'session changed' actually prints on console. but sess:eI6Jnj0gzMkdEvszUciqbcJ9 is not updated in database. I'm presuming, express does not permit redisClient.set to change the session data. How may i achieve it?
PS. The change works on my localhost but does not work, once i use redistogo on heroku.
Edit:-
I check the key not updating, by runnign get 'sess:eI6Jnj0gzMkdEvszUciqbcJ9' command on the console. i am using redis.createClient
var redisClient = require('redis').createClient( 3374, 'birdeye.redistogo.com', {detect_buffers: true});
redisClient.auth('052cb8c4142g202f7878588dd5fb129', function() {
console.log('Redis client connected');
});
I am only guessing that express.js may be 'updating its session automatically Probably, it simply overwrites all your changes with req.session.'

Socket Hang Up when using https.request in node.js

When using https.request with node.js v04.7, I get the following error:
Error: socket hang up
at CleartextStream.<anonymous> (http.js:1272:45)
at CleartextStream.emit (events.js:61:17)
at Array.<anonymous> (tls.js:617:22)
at EventEmitter._tickCallback (node.js:126:26)
Simplified code that will generate the error:
var https = require('https')
, fs = require('fs')
var options = {
host: 'localhost'
, port: 8000
, key: fs.readFileSync('../../test-key.pem')
, cert: fs.readFileSync('../../test-cert.pem')
}
// Set up server and start listening
https.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('success')
}).listen(options.port, options.host)
// Wait a second to let the server start up
setTimeout(function() {
var clientRequest = https.request(options, function(res) {
res.on('data', function (chunk) {
console.log('Called')
})
})
clientRequest.write('')
clientRequest.end()
}, 1000)
I get the error even with the server and client running on different node instances and have tested with port 8000, 3000, and 443 and with and without the SSL certificates. I do have libssl and libssl-dev on my Ubuntu machine.
Any ideas on what could be the cause?
In
https.createServer(function (req, res) {
you are missing options when you create the server, should be:
https.createServer(options, function (req, res) {
with your key and cert inside
I had a very similar problem where the response's end event never fired.
Adding this line fixed the problem:
// Hack to emit end on close because of a core bug that never fires end
response.on('close', function () {response.emit('end')});
I found an example of this in the request library mentioned in the previous answer.
Short answer: Use the the latest source code instead of the one you have. Store it where you will and then require it, you are good to go.
In the request 1.2.0 source code, main.js line 76, I see
http.createClient(options.uri.port, options.uri.hostname, options.uri.protocol === 'https:');
Looking at the http.js source code, I see
exports.createClient = function(port, host) {
var c = new Client();
c.port = port;
c.host = host;
return c;
};
It is requesting with 3 params but the actual function only has 2. The functionality is replaced with a separate module for https.
Looking at the latest main.js source code, I see dramatic changes. The most important is the addition of require('https').
It appears that request has been fixed but never re-released. Fortunately, the fix seems to work if you just copy manually from the raw view of the latest main.js source code and use it instead.
I had a similar problem and i think i got a fix. but then I have another socket problem.
See my solution here: http://groups.google.com/group/nodejs/browse_thread/thread/9189df2597aa199e/b83b16c08a051706?lnk=gst&q=hang+up#b83b16c08a051706
key point: use 0.4.8, http.request instead of http.createClient.
However, the new problem is, if I let the program running for long time, (I actually left the program running but no activity during weekend), then I will get socket hang up error when I send a request to http Server. (not even reach the http.request). I don't know if it is because of my code, or it is different problem with http Server