Migrate Express 2.5 app to Express 4.12

I am using the code found in this repo.

I want to migrate from Express 2.5 to 4.12 so I have taken the original Express 2.5 app.js and tried to integrate it with the new Express 4.12 app.js:

var express = require('express');
var ExpressBrute = require('express-brute');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var arr = require('./compiler/compilers');
var sandBox = require('./compiler/DockerSandbox');

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

var app = express();

var store = new ExpressBrute.MemoryStore(); // stores state locally, don't use this in production
var bruteforce = new ExpressBrute(store,{
  freeRetries: 50,
  lifetime: 3600
});

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

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components',  express.static(path.join(__dirname, '/bower_components')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

app.all('*', function(req, res, next)
{
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type');

  next();
});

function random(size) {
  //returns a crypto-safe random
  return require("crypto").randomBytes(size).toString('hex');
}


app.post('/compile',bruteforce.prevent,function(req, res)
{

  var language = req.body.language;
  var code = req.body.code;
  var stdin = req.body.stdin;

  var folder= 'temp/' + random(10); //folder in which the temporary folder will be saved
  var path=__dirname+"/"; //current working path
  var vm_name='virtual_machine'; //name of virtual machine that we want to execute
  var timeout_value=20;//Timeout Value, In Seconds

  //details of this are present in DockerSandbox.js
  var sandboxType = new sandBox(timeout_value,path,folder,vm_name,arr.compilerArray[language][0],arr.compilerArray[language][1],code,arr.compilerArray[language][2],arr.compilerArray[language][3],arr.compilerArray[language][4],stdin);


  //data will contain the output of the compiled/interpreted code
  //the result maybe normal program output, list of error messages or a Timeout error
  sandboxType.run(function(data,exec_time,err)
  {
    //console.log("Data: received: "+ data)
    res.send({output:data, langid: language,code:code, errors:err, time:exec_time});
  });

});


//app.use('/editor', function(req, res)
//{
//  res.render('editor');
//});


module.exports = app;

If I run then start express DEBUG=myapp npm start it all works fine except when I go to compile code in the app itself (refer repo) when a post request is fired I receive a 404 in response, I am also getting a 404 in the developer console. Any ideas, I assume the error is around this line of code:

app.post('/compile',bruteforce.prevent,function(req, res)


Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.