Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. var express = require('express');
  2. //var bodyparser = require('body-parser')
  3. var mysql = require('mysql');
  4. var BigInteger = require("big-integer");
  5. var port = process.env.PORT || 3000;
  6.  
  7. var app = express();
  8. //app.use(bodyparser.json())
  9.  
  10. var knex = require('knex')({
  11. dialect: 'mysql',
  12. connection: {
  13. host: 'localhost',
  14. user: 'root',
  15. password: '',
  16. database: 'mtis'
  17. }
  18. });
  19.  
  20. app.listen(port, function(){
  21. console.log("Servidor arrancado");
  22. })
  23.  
  24. module.exports = app;
  25.  
  26. //Comprobar RestKey middleware
  27. function checkRestKey(pet, resp, next){
  28. var key = pet.headers.restkey
  29.  
  30. if(key){
  31. knex('restkey').where('restKey', key)
  32. .then(function(restKey){
  33. if (restKey != "") {
  34. next()
  35. }
  36. else {
  37. resp.status(401)
  38. resp.send({
  39. error: "RestKey no válida"
  40. })
  41. }
  42. })
  43. }
  44. else {
  45. resp.status(403)
  46. resp.send({
  47. message: 'No se ha proporcionado RestKey'
  48. })
  49. }
  50. }
  51.  
  52. //Página de bienvenida
  53. app.get('/api', function(pet, resp){
  54. resp.status(200)
  55. resp.send("Bienvenido a Practica 2 MTIS")
  56. })
  57.  
  58. //Mostrar la info de un código postal
  59. app.get('/api/codigosPostales/:codigo', function(pet, resp){
  60. var codigo = pet.params.codigo
  61.  
  62. knex('codigospostales').where('codigoPostal', codigo)
  63. .then(function(codPostal){
  64. if (codPostal != "") {
  65. resp.status(200)
  66. resp.send({
  67. codigoPostal: codPostal[0].codigoPostal,
  68. poblacion: codPostal[0].poblacion,
  69. provincia: codPostal[0].provincia,
  70. existe: true
  71. })
  72. }
  73. else {
  74. resp.status(404)
  75. resp.send({
  76. message: "El código postal no se ha encontrado",
  77. existe: false
  78. })
  79. }
  80. })
  81. })
  82.  
  83. //Validar un NIF
  84. app.get('/api/nif/:nif', checkRestKey, function(pet, resp){
  85. var nif = pet.params.nif
  86. var key = pet.params.restKey
  87.  
  88. var patron = /^[0-9]{8}[a-zA-Z]{1}$/
  89. var valido = patron.test(nif)
  90.  
  91. resp.status(200)
  92. resp.send({
  93. valido: valido
  94. })
  95. })
  96.  
  97. //Validar un IBAN
  98. app.get('/api/iban/:iban', checkRestKey, function(pet, resp){
  99. //Se pasa a Mayusculas
  100. IBAN = pet.params.iban.toUpperCase();
  101. //Se quita los blancos de principio y final.
  102. IBAN = IBAN.trim();
  103. IBAN = IBAN.replace(/\s/g, ""); //Y se quita los espacios en blanco dentro de la cadena
  104.  
  105. var letra1,letra2,num1,num2;
  106. var isbanaux;
  107. var numeroSustitucion;
  108. //La longitud debe ser siempre de 24 caracteres
  109. if (IBAN.length != 24) {
  110. resp.status(400)
  111. resp.send({
  112. valido: false,
  113. error: "Introduce un IBAN con formato correcto"
  114. })
  115. }
  116. else {
  117. // Se coge las primeras dos letras y se pasan a números
  118. letra1 = IBAN.substring(0, 1);
  119. letra2 = IBAN.substring(1, 2);
  120. num1 = getnumIBAN(letra1);
  121. num2 = getnumIBAN(letra2);
  122. //Se sustituye las letras por números.
  123. isbanaux = String(num1) + String(num2) + IBAN.substring(2);
  124. // Se mueve los 6 primeros caracteres al final de la cadena.
  125. isbanaux = isbanaux.substring(6) + isbanaux.substring(0,6);
  126.  
  127. //Se calcula el resto, llamando a la función modulo97, definida más abajo
  128. resto = modulo97(isbanaux);
  129. if (resto == 1){
  130. resp.status(200)
  131. resp.send({
  132. valido: true
  133. })
  134. }else{
  135. resp.status(200)
  136. resp.send({
  137. valido: false,
  138. error: "IBAN incorrecto"
  139. })
  140. }
  141. }
  142. })
  143.  
  144. function modulo97(iban) {
  145. var parts = Math.ceil(iban.length/7);
  146. var remainer = "";
  147.  
  148. for (var i = 1; i <= parts; i++) {
  149. remainer = String(parseFloat(remainer+iban.substr((i-1)*7, 7))%97);
  150. }
  151.  
  152. return remainer;
  153. }
  154.  
  155. function getnumIBAN(letra) {
  156. ls_letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  157. return ls_letras.search(letra) + 10;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement