Skip to content

Commit

Permalink
refactor(react api): ES6 migration
Browse files Browse the repository at this point in the history
Related OWASP#192
  • Loading branch information
KoolTheba committed Mar 21, 2020
1 parent 9ba3401 commit 0efe7f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 66 deletions.
20 changes: 10 additions & 10 deletions apps/react-apirest/api/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

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

var app = express();
const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -23,12 +23,12 @@ app.use('/', indexRouter);
app.use('/users', usersRouter);

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

// error handler
app.use(function(err, req, res, next) {
app.use((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 : {};
Expand Down
61 changes: 11 additions & 50 deletions apps/react-apirest/api/bin/www
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('api:server');
var http = require('http');
var config = require('../config');

/**
* Get port from environment and store in Express.
*/
const app = require('../app');
const debug = require('debug')('api:server');
const http = require('http');
const { port } = require('../config');

var port = normalizePort(config.port);
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

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

const server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

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

function normalizePort(val) {
var 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.
Expand All @@ -59,18 +22,16 @@ function onError(error) {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
const bind = `${typeof port === 'string' ? 'Pipe' : 'Port'} ${port}`;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
Expand All @@ -83,9 +44,9 @@ function onError(error) {
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
debug(`Listening on ${bind}`);
}
6 changes: 3 additions & 3 deletions apps/react-apirest/api/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var express = require('express');
var router = express.Router();
const express = require('express');
const router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
router.get('/', (req, res, next) => {
res.render('index', { title: 'Express' });
});

Expand Down
6 changes: 3 additions & 3 deletions apps/react-apirest/api/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var express = require('express');
var router = express.Router();
const express = require('express');
const router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
router.get('/', (req, res, next) => {
res.send('respond with a resource');
});

Expand Down

0 comments on commit 0efe7f0

Please sign in to comment.