Skip to content

Commit

Permalink
curso-node-restserver-basico
Browse files Browse the repository at this point in the history
  • Loading branch information
wendell-nasc committed Jun 20, 2021
0 parents commit 6e4a5b3
Show file tree
Hide file tree
Showing 10 changed files with 572 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8080
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


node_modules/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# WebServer + RestServer

Recuerden que deben de ejecutar ```npm install``` para reconstruir los módulos de Node.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require('dotenv').config();
const Server = require('./models/server');


const server = new Server();



server.listen();
60 changes: 60 additions & 0 deletions controllers/usuarios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { response, request } = require('express');


const usuariosGet = (req = request, res = response) => {

const { q, nombre = 'No name', apikey, page = 1, limit } = req.query;

res.json({
msg: 'get API - controlador',
q,
nombre,
apikey,
page,
limit
});
}

const usuariosPost = (req, res = response) => {

const { nombre, edad } = req.body;

res.json({
msg: 'post API - usuariosPost',
nombre,
edad
});
}

const usuariosPut = (req, res = response) => {

const { id } = req.params;

res.json({
msg: 'put API - usuariosPut',
id
});
}

const usuariosPatch = (req, res = response) => {
res.json({
msg: 'patch API - usuariosPatch'
});
}

const usuariosDelete = (req, res = response) => {
res.json({
msg: 'delete API - usuariosDelete'
});
}




module.exports = {
usuariosGet,
usuariosPost,
usuariosPut,
usuariosPatch,
usuariosDelete,
}
46 changes: 46 additions & 0 deletions models/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const express = require('express');
const cors = require('cors');

class Server {

constructor() {
this.app = express();
this.port = process.env.PORT;
this.usuariosPath = '/api/usuarios';

// Middlewares
this.middlewares();

// Rutas de mi aplicación
this.routes();
}

middlewares() {

// CORS
this.app.use( cors() );

// Lectura y parseo del body
this.app.use( express.json() );

// Directorio Público
this.app.use( express.static('public') );

}

routes() {
this.app.use( this.usuariosPath, require('../routes/usuarios'));
}

listen() {
this.app.listen( this.port, () => {
console.log('Servidor corriendo en puerto', this.port );
});
}

}




module.exports = Server;
Loading

0 comments on commit 6e4a5b3

Please sign in to comment.