Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- //var bodyparser = require('body-parser')
- var mysql = require('mysql');
- var BigInteger = require("big-integer");
- var port = process.env.PORT || 3000;
- var app = express();
- //app.use(bodyparser.json())
- var knex = require('knex')({
- dialect: 'mysql',
- connection: {
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'mtis'
- }
- });
- app.listen(port, function(){
- console.log("Servidor arrancado");
- })
- module.exports = app;
- //Comprobar RestKey middleware
- function checkRestKey(pet, resp, next){
- var key = pet.headers.restkey
- if(key){
- knex('restkey').where('restKey', key)
- .then(function(restKey){
- if (restKey != "") {
- next()
- }
- else {
- resp.status(401)
- resp.send({
- error: "RestKey no válida"
- })
- }
- })
- }
- else {
- resp.status(403)
- resp.send({
- message: 'No se ha proporcionado RestKey'
- })
- }
- }
- //Página de bienvenida
- app.get('/api', function(pet, resp){
- resp.status(200)
- resp.send("Bienvenido a Practica 2 MTIS")
- })
- //Mostrar la info de un código postal
- app.get('/api/codigosPostales/:codigo', function(pet, resp){
- var codigo = pet.params.codigo
- knex('codigospostales').where('codigoPostal', codigo)
- .then(function(codPostal){
- if (codPostal != "") {
- resp.status(200)
- resp.send({
- codigoPostal: codPostal[0].codigoPostal,
- poblacion: codPostal[0].poblacion,
- provincia: codPostal[0].provincia,
- existe: true
- })
- }
- else {
- resp.status(404)
- resp.send({
- message: "El código postal no se ha encontrado",
- existe: false
- })
- }
- })
- })
- //Validar un NIF
- app.get('/api/nif/:nif', checkRestKey, function(pet, resp){
- var nif = pet.params.nif
- var key = pet.params.restKey
- var patron = /^[0-9]{8}[a-zA-Z]{1}$/
- var valido = patron.test(nif)
- resp.status(200)
- resp.send({
- valido: valido
- })
- })
- //Validar un IBAN
- app.get('/api/iban/:iban', checkRestKey, function(pet, resp){
- //Se pasa a Mayusculas
- IBAN = pet.params.iban.toUpperCase();
- //Se quita los blancos de principio y final.
- IBAN = IBAN.trim();
- IBAN = IBAN.replace(/\s/g, ""); //Y se quita los espacios en blanco dentro de la cadena
- var letra1,letra2,num1,num2;
- var isbanaux;
- var numeroSustitucion;
- //La longitud debe ser siempre de 24 caracteres
- if (IBAN.length != 24) {
- resp.status(400)
- resp.send({
- valido: false,
- error: "Introduce un IBAN con formato correcto"
- })
- }
- else {
- // Se coge las primeras dos letras y se pasan a números
- letra1 = IBAN.substring(0, 1);
- letra2 = IBAN.substring(1, 2);
- num1 = getnumIBAN(letra1);
- num2 = getnumIBAN(letra2);
- //Se sustituye las letras por números.
- isbanaux = String(num1) + String(num2) + IBAN.substring(2);
- // Se mueve los 6 primeros caracteres al final de la cadena.
- isbanaux = isbanaux.substring(6) + isbanaux.substring(0,6);
- //Se calcula el resto, llamando a la función modulo97, definida más abajo
- resto = modulo97(isbanaux);
- if (resto == 1){
- resp.status(200)
- resp.send({
- valido: true
- })
- }else{
- resp.status(200)
- resp.send({
- valido: false,
- error: "IBAN incorrecto"
- })
- }
- }
- })
- function modulo97(iban) {
- var parts = Math.ceil(iban.length/7);
- var remainer = "";
- for (var i = 1; i <= parts; i++) {
- remainer = String(parseFloat(remainer+iban.substr((i-1)*7, 7))%97);
- }
- return remainer;
- }
- function getnumIBAN(letra) {
- ls_letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- return ls_letras.search(letra) + 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement