Skip to content
Snippets Groups Projects
Commit a2334768 authored by Mariana Molina's avatar Mariana Molina
Browse files

Arreglo problema con API POST

parent 843ce3af
Branches
Tags
No related merge requests found
{ {
"_from": "body-parser", "_from": "body-parser@^1.19.0",
"_id": "body-parser@1.19.0", "_id": "body-parser@1.19.0",
"_inBundle": false, "_inBundle": false,
"_integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", "_integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
"_location": "/body-parser", "_location": "/body-parser",
"_phantomChildren": {}, "_phantomChildren": {},
"_requested": { "_requested": {
"type": "tag", "type": "range",
"registry": true, "registry": true,
"raw": "body-parser", "raw": "body-parser@^1.19.0",
"name": "body-parser", "name": "body-parser",
"escapedName": "body-parser", "escapedName": "body-parser",
"rawSpec": "", "rawSpec": "^1.19.0",
"saveSpec": null, "saveSpec": null,
"fetchSpec": "latest" "fetchSpec": "^1.19.0"
}, },
"_requiredBy": [ "_requiredBy": [
"#USER", "#USER",
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
], ],
"_resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", "_resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
"_shasum": "96b2709e57c9c4e09a6fd66a8fd979844f69f08a", "_shasum": "96b2709e57c9c4e09a6fd66a8fd979844f69f08a",
"_spec": "body-parser", "_spec": "body-parser@^1.19.0",
"_where": "C:\\Users\\Usuario\\Desktop\\Laboratorio02", "_where": "C:\\Users\\Usuario\\Desktop\\Laboratorio02",
"bugs": { "bugs": {
"url": "https://github.com/expressjs/body-parser/issues" "url": "https://github.com/expressjs/body-parser/issues"
......
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser');
// ----------------- Initializations ----------------- // ----------------- Initializations -----------------
// Importo librería express // Importo librería express
...@@ -8,6 +9,9 @@ require('./database'); ...@@ -8,6 +9,9 @@ require('./database');
// ----------------- Settings ----------------- // ----------------- Settings -----------------
app.set('port', 3000); app.set('port', 3000);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ----------------- Middlewares ----------------- // ----------------- Middlewares -----------------
// ----------------- Global variables ----------------- // ----------------- Global variables -----------------
......
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser');
const router = express.Router();
const app = express(); const app = express();
app.use(bodyParser.urlencoded({ extended: false })); const router = express.Router();
app.use(bodyParser.json());
const users = []; const users = [
{
email: 'email1',
comment: []
}
];
// [GET] OBTENER TODOS LOS USUARIOS: /users // [GET] OBTENER TODOS LOS USUARIOS: /users
router.get('/users', (req, res) => { router.get('/users', (req, res) => {
...@@ -16,10 +18,10 @@ router.get('/users', (req, res) => { ...@@ -16,10 +18,10 @@ router.get('/users', (req, res) => {
// [GET] OBTENER UN USUARIO: /user/:id // [GET] OBTENER UN USUARIO: /user/:id
router.get('/user/:id', (req, res) => { router.get('/user/:id', (req, res) => {
const userId = Number(req.params.id); const userEmail = req.params.id;
const user = users.find((data) => data.id === userId); const user = users.find((data) => data.email === userEmail);
if (!user) { if (!user) {
res.status(500).send('Usuario no encontrado') res.status(500).send('Usuario no encontrado');
} else { } else {
res.json(user); res.json(user);
} }
...@@ -27,12 +29,19 @@ router.get('/user/:id', (req, res) => { ...@@ -27,12 +29,19 @@ router.get('/user/:id', (req, res) => {
// [POST] CREAR UN NUEVO USUARIO: /user/new // [POST] CREAR UN NUEVO USUARIO: /user/new
router.post('/user', (req, res) => { router.post('/user', (req, res) => {
console.log(req); if (req.body) {
console.log(req.body); console.log(req.body);
const newUser = {email: req.body.email, comment: [] }; if (req.body.email) {
const newUser = {email: req.body.email.toString(), comment: [] };
console.log(newUser); console.log(newUser);
users.push(newUser); users.push(newUser);
res.json(users); res.json(users);
} else {
res.status(500).send('Se debe ingresar un email');
}
} else {
res.status(500).send('Existió un error al procesar la solicitud');
}
}); });
// [GET] OBTENER LOS COMENTARIOS DE UN USUARIO: /user/:id/comments // [GET] OBTENER LOS COMENTARIOS DE UN USUARIO: /user/:id/comments
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment