I'm a little lost with this task, since it's my first experience with node.js application. The following code corresponds to a small server that listens on the port 3000 requests of different android devices that run an app that does the solitude to the small server, and it performs a query in a DB of MYSQL and sends them to along with some files .jpg.
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(__dirname + '/images'));
var pool = mysql.createPool({
connectionLimit : 1000,
host : 'localhost',
user : 'root',
password : '',
database : 'app_mt8442',
debug : false
});
app.use(router);
app.listen(3000, function() {
console.log("Node server running on http://localhost:3000");
});
router.get('/api/images', function (req, res)
{
pool.getConnection(function(err,connection){
if (err) {
res.status(500).send(err);
console.log('Error Conectando a Base de Datos');
return;
}
console.log('connected as id ' + connection.threadId);
var query = "SELECT * from images order by name" ;
connection.query(query,function(err,rows){
if(!err) {
res.header("Access-Control-Allow-Origin", "*");
res.status(200).send(rows);
}
else{
res.header("Access-Control-Allow-Origin", "*");
res.status(500).send({"updated":false, "error":err});
}
});
connection.release();
});
});
I understand that I have to use a connection pool, not multiple connections for every android device that makes a request. I'm right? What I need now, is to do this same with a Microsoft SQL Server DB. I understand that I should use node-mssql for the task, but I have no idea how to do it. If anyone can help me I will thank you very much. Thank you for reading.
via M. Gleria
No comments:
Post a Comment