Commit 5f515911 authored by Ivan Tancara's avatar Ivan Tancara
Browse files

adicion de backend

parent a62ec092
/node_modules
\ No newline at end of file
'use strict';
const config = {
autorizacion: 'AAAAPEVgJjw:APA91bFN3ZmibnDgcs6YOyf8CgwxF0ZPkUlZvncHkuFsIozznmYAcvKRnR7rFyDIFpYdntMZzbzWj4QYtOXGjXdxmRt0R5jFfw15LCXASK9fJCqdoItWb4dcdiDRGifQUOeePDdyVq_v',
usuarios: [
{
usuario: 'admin',
password: '123456'
}
]
};
module.exports = config;
const express = require('express');
const config = require('./config.js');
const axios = require('axios');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const corsOptions = {
origin: '*' // PONER UNA URL
}
app.use(bodyParser.json({ limit: '10mb' }));
app.use(cors(corsOptions));
async function mensajeExito (res, datos) {
return res.status(200).json({
finalizado: true,
mensaje: 'OK',
datos: datos
})
}
async function mensajeError (res, mensajeError) {
return res.status(400).json({
finalizado: false,
mensaje: mensajeError,
datos: null
})
}
app.post('/login', async function (req, res) {
try {
console.log('___________________________________________________________________________________________________');
console.log(req.body);
console.log('¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯');
const { usuario, password } = req.body
let existe = false;
for (const usuarioConfig of config.usuarios) {
if (usuarioConfig.usuario === usuario && usuarioConfig.password === password ) {
existe = true;
}
}
if (!existe) {
throw new Error('El usuario no existe');
}
mensajeExito(res, { usuario, login: existe });
} catch (error) {
mensajeError(res, error.message);
}
});
app.post ('/send', async function (req, res) {
try {
const { titulo, text} = req.body;
const init = {
method: 'POST',
url: 'https://fcm.googleapis.com/fcm/send',
headers : {
Authorization: `key=${config.autorizacion}`,
'content-type': 'application/json'
},
data: {
notification: {
title: titulo,
text: text,
badge: 1,
sound: 'default'
},
data: {
foo: 'bar'
},
priority: 'High',
to: 'DEVICE_TOKEN'
}
}
const repsuesta = await axios(init);
mensajeExito(res, repsuesta.data);
} catch (error) {
console.log('___________________________________________________________________________________________________');
console.log(error);
console.log('¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯');
mensajeError(res, error.message);
}
})
app.listen(5000, function () {
console.log('App running on port 5000!');
});
This diff is collapsed.
{
"name": "proyecto",
"version": "1.0.0",
"description": "proyecto desconocido",
"main": "src/application/index.js",
"scripts": {
"start:dev": "nodemon backend/server.js",
"start": "node backend/server.js"
},
"dependencies": {
"axios": "^0.19.0",
"cors": "^2.8.5",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"moment": "^2.24.0"
},
"devDependencies": {
"nodemon": "^2.0.1"
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment