1. Where is the exact location to implement the db connection, release code? 2. How to process user information security, such as db or email access information

Asked 1 years ago, Updated 1 years ago, 68 views

1.

var mysql = require('mysql');
var conn = mysql.createConnection(dbconfig.connection);
conn.connect(function(err) {
    if (err) {
      console.log('Error connecting: ' + err.stack);
      return;
    }

    console.log('connected as id ' + conn.threadId);
  });

  conn.query('USE ' + dbconfig.database);

Write the above code first in the .js file

conn.end(); // or
conn.release();

End db processing with .

By the way,

Error: Cannot enqueue Query after invoking quit.

The same error occurred, so I think I opened the db connection and closed it incorrectly.

2.

When developing the web (node.js), how should I handle security related to login information?

For example,

When using the nodemailer module

var smtpTransport = nodemailer.createTransport('SMTP', {
        service : 'Gmail',
        auth : {
          user : '[email protected]',
          pass : 'email1234!'
        }
      });

Is it a method that does not have any security problems simply storing user, pass information in a .js file,

Alternatively, when using the mysql module,

module.exports = {
    'connection': {
        'host': '127.0.0.1',
        'user': 'root',
        'password': 'admin1234'
    },
    'database': 'test',
  'user_table': 'tb_test'
};

Can I save it as a .js file in the config format and load it and use it?

I am curious about processing user information when developing the web.

1. Processing location or method for disconnecting db when using mysql module

2.How to handle security for db or email access user information

node.js mysql config

2022-09-22 21:41

1 Answers

To disconnect db from MySQL, release the connection within query function.

For example:

conn.query('select * from users', function (err, rows) {
    if(err) {
        conn.release();
        throw err;
    }

    // ...

    conn.release();
});

Remember to return the connection in the event of an error.^

^

The second question is http://stackoverflow.com/questions/22348705/best-way-to-store-db-config-in-node-js-express-app

Please refer to ^

^


2022-09-22 21:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.