Connecting to a Database
Overview
For security reasons, web browsers usually cannot connect directly to a database.
(For information about connecting to a database from Javascript running outside the browser,
please see
node database) The best way to retrieve data from a database
is to have a webserver act as a proxy which will pass on any query issued by the browser to the database and return the results to the
calling browser.
The query is typically passed as a parameter in the HTTP POST. Using the
$ajax service, the following code would demonstrate
passing a query in the POST parameters:
let data = await $ajax({
url:'http://proxy-server/',
data:{
query:`select * from Table where age > 20;`
}
});
Sample Webserver
Using
node.js, it is a simple matter to implement a webserver that can connect to a variety of databases inclucing
MySql, Sql Server and Oracle. As a sample, we provide a simple implementation of a webserver that will connect to a
local Sql Server database.
Sample Webserver