Skip to content

Commit

Permalink
express
Browse files Browse the repository at this point in the history
  • Loading branch information
Loda00 committed Feb 1, 2022
0 parents commit 66e0ee2
Show file tree
Hide file tree
Showing 14 changed files with 1,617 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
101 changes: 101 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const express = require('express')
const { v4: uuid } = require('uuid')

const PORT = 8080
const app = express()

const data = []

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get('/list', (request, response) => {
// const { body, query, params, url } = request
// const { cod } = params
// const { name, age } = query
// console.log('query: ', url)

// response.write('Node.js v14')

// const json = {
// status: cod,
// auth: true,
// response: {
// name,
// age: Number(age)
// }
// }

response.status(200).json(data)
})

app.post('/list', (request, response) => {
const { body } = request

const json = {
status: 200,
auth: true,
response: {
id: uuid(),
...body
}
}

data.push(json.response)

response.status(200).json(json)
})

app.put('/list/:id', (request, response) => {

const { params, body } = request
const { id } = params

const dataUpdated = data.map((item) => {
if (item.id === id) {
return {
id,
...body
}
}
return item
})

data.splice(0, data.length)

data.push(...dataUpdated)

const json = {
status: 200,
auth: true,
response: dataUpdated.find((item) => item.id === id)
}

response.status(200).json(json)

})

app.delete('/list/:id', (req, res) => {
const { params } = req

const dataUpdated = data.filter((item) => {
if (item.id === params.id) {
return false
}
return item
})

data.splice(0, data.length)

data.push(...dataUpdated)

res.status(200).json({
status: 200,
message: 'Se eliminó el registro con el código ' + params.id
})

})

app.listen(PORT, () => {
console.log(`El servidor está escuchando en el puerto ${PORT}`)
})
41 changes: 41 additions & 0 deletions demo-generator/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions demo-generator/app/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

const app = require('../app');
const debug = require('debug')('app:server');
const http = require('http');

/**
* Get port from environment and store in Express.
*/

const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

const server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Loading

0 comments on commit 66e0ee2

Please sign in to comment.