Ppb2011 1
var mysql = require('mysql');
//
var http = require('http');
var url = require('url');
 
var server = http.createServer(function(req, res) {
  query = decodeURIComponent(url.parse(req.url).pathname).split('/')[1];//получаем sql запрос
  console.log(query);
 
   //Если запрос состоит из строки 'kill', завершаем работу сервиса
    if (query === 'kill') {
      res.writeHead(200, {"Content-Type": "text/html; charset=UTF-8"});
      res.end("<h1>Server кирдык<h1>");
      server.close('');
      req.connection.destroy('');
      console.log("кирдык");
      process.exit('');
      return;
    }
 
  res.writeHead(200, {"Content-Type": "application/json; charset=UTF-8","Access-Control-Allow-Origin":"*",
                      "Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept"});
 
  var sql = query;
  var connection = mysql.createConnection({
  host: 'airs.ce5k6a6fvfk2.us-east-1.rds.amazonaws.com',
  user: 'userairs',
  password: 'userairs',
  database : 'airs'
});
connection.connect();
//выполняем запрос
connection.query(sql,
  function(err, rows, fields) {
    connection.end();
    if (err) 
    {
      console.log('Неверный запрос');
      res.writeHead(200, {"Content-Type": "text/html; charset=UTF-8"});
      res.end("<h1>Неверный запрос<h1>");
    }
    else
    {
      //формируем ответ
      author ={"autor":"is10-99", "script":"C:\\Dropbox\\edunet\\2014\\is10-99\\nodejsXmlServer\\coffee\\XmlServer.coffee", "versionOf":"2016-05-12", "/SQL":query};
      var arr = [author, rows];
      res.end(JSON.stringify(arr));
    }
  }
);
});
server.listen(8080);
console.log('Сервер стартовал на порту 8080')