using sql querys with an express app code example
Example 1: mssql query node js
const sql = require('mssql')
const query=`select * from table`;
config = {
database: "dbname",
options: {
enableArithAbort: true,
encrypt: true,
rowCollectionOnRequestCompletion: false,
},
parseJSON: true,
password: "password",
port: port,
server: "server ip or name",
user: "usenme",
};
sql.on('error', err => {
})
sql.connect(config).then(pool => {
return pool.request()
.query(query)
}).then(result => {
console.log(result.recordset)
}).catch(err => {
console.log(err)
});
Example 2: how to access a database in express
var mysql = require('mysql')
var connection = mysql.createConnection({
host: 'localhost',
user: 'dbuser',
password: 's3kreee7',
database: 'my_db'
})
connection.connect()
connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err
console.log('The solution is: ', rows[0].solution)
})
connection.end()