Q:

Salesforce api session expired, how to reconnect automatically

I'm using the node module jsforce to add leads to a salesforce leads table. https://www.npmjs.org/package/node-salesforce here's the docs. The website is live at the moment but every day I'm having to restart the server as the session expires, I get this message 
{ [INVALID_SESSION_ID: Session expired or invalid] name: 'INVALID_SESSION_ID', errorCode: 'INVALID_SESSION_ID' } undefined

here is the section of code that sets it off:
conn.sobject("Lead").create({
  email : req.body.email,
  firstname : req.body.first_name,
  lastname : req.body.last_name,
  title : req.body.job_title,
  company : req.body.company,
  leadsource: 'Clearing Microsite',
  description: req.body.message
}, function(err, ret) {
  if (err || !ret.success) { 
    return console.error(err, ret);
  }
console.log("Created record id : " + ret.id);
  });
});

I'm guessing the return console.error(err, ret) is what's catching the session expiring, but how can I reconnect after this occurs? 
I've tried using conn.logout then re logging in, with the login function but couldn't get it to work and it's rather hard to test as it seems to only expire after a day!
Edit:
I think I'm needing something like if (err == {[INVALID...]}) {reconnect} But I just have no idea what to use

A:

Think I may have solved it, within the if (err || !ret.success) {} I added:
if (err.name == 'INVALID_SESSION_ID') {
  conn.logout(function (err) {
    if (err) { return console.error(err); }
    conn.login(*login details*)
      // then repeated the lead entry
  });
}

I cant check to see if this works until tomorrow morning when the session times out, but will update if it does work / doesn't
Edit:
Didn't work, going to try without the logout. got this error 
[Error: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: <KEY> This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. ]

Second Edit: Taking out the logout worked like a charm! Thanks @superfell

