This example demonstrates how to connect to different databases (MySQL, Firebird, MS SQL, and PostgreSQL). The files of data adapters are located in the directory with this example. You can include adapters into your projects without any changes.
First, you should connect all the required modules and data adapters, this is done using the
require()
standard function:
var http = require("http");
var adapter = require("stimulsoft-data-adapter");
Next, define the
accept()
main function that will work with adapters - load all data from the request and run the adapter to execute the received command. Define the
onProcess()
callback function to process the result:
function accept(request, response) {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Cache-Control", "no-cache");
var data = "";
request.on('data', function (buffer) {
data += buffer;
});
request.on('end', function () {
var command = adapter.getCommand(data);
adapter.process(command, function (result) {
var responseData = getResponse(result);
response.end(responseData);
});
});
}
All is ready, start the server and specify the required port, in this case, 9615:
http.createServer(accept).listen(9615);
In the screenshot below you can see the result of the sample code: