Below code is belongs to index.js
file.
When I go to the link "localhost:300/admins/" according to the code it should connect with SQL Server and get back the result on console.
My Microsoft SQL Server Management Studio 2012 is running well and from Visual Studio it can operate smoothly.
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const hbs = require('hbs');
const sql = require('mssql');
const sqlConfig = {
user: 'xxx',
password: 'xxxx',
database: 'xxxxx',
server: '127.0.0.1',
port: xxxx,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
};
// static file's path set up
app.use(express.static('public'));
app.use('/css', express.static(__dirname + '/public'));
app.use('/images', express.static(__dirname + '/public'));
app.use('/js', express.static(__dirname + '/public'));
const pagespath = path.join(__dirname, './templates/views');
const partialspath = path.join(__dirname, './templates/partials');
// Set Views and view engine
app.set('view engine', 'hbs');
app.set('views', pagespath);
hbs.registerPartials(partialspath);
app.get('/', (req, res) => {
res.render('home', { title: 'Home' });
});
app.get('/admins', function (req, res) {
var result;
async () => {
try {
const pool = await sql.ConnectionPool(config);
const result = await pool.query`select name from tbl_info_p_admin`;
res._write(result);
console.log(result);
} catch (err) {
console.log(err);
}
await sql.close();
};
res.render('./admin/masters', { title: 'ADMIN' });
});
app.listen(process.env.PORT || 3000, function (err) {
if (err) {
console.log(err);
} else {
console.log('Server Started At Port 3000');
}
});