Using child_process.exec to send a command returning 404

244 views Asked by At

I have a simple server setup using NodeJs, from which I would like to send a command to the terminal on the server.

I'm trying to use child_process.exec to send a command, which when I run from the terminal works correctly.

The issue I'm having is that the POST method /unLock is not found. as a response i get.

POST /unLock 404 1.770 ms - 1186

I've included the code for the server and routes/views I have set up below, I'm new to node and trying to learn so any help would be appreciated.

app.js

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

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

var util = require('util')
//var exec = require('child_process').exec;
var app = express();

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


app.get('/javascript/jquery.min.js', function (req, res) {
        res.sendFile( __dirname + "/javascript" + "/jquery.min.js" );

});

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

app.use('/', index);
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);
});

app.post('/', function(req, res) {
  console.log(req.body);
  res.send(200);

  });

//trying to send command here
//recieiving 404
app.post('/unLock', function (req, res) {
exec("ls -la", {stdio:[0,1,2]});
res.send(200);

console.log("button clicked");
});

// 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;

index.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res){
  res.render('index', {
    title: 'Home'
  });
});

router.get('/login', function(req, res){
  res.render('login', {
    title: 'login'
  });
});

router.get('/unLock', function(req, res){
    console.log("hello"); //this doesnt work 
});

router.get('/contact', function(req, res){
  res.render('contact', {
    title: 'Contact'
  });
});

module.exports = router;

index.ejs

<!DOCTYPE html>
<html>
<script language="JavaScript" type="text/javascript" src="/javascripts/jquery.min.js"></script>
<script>

function unLock() { 
  $.ajax({
    type: 'POST',
                url: '/unLock',
    data: '',
    success: function (data) {
                }
       });
}
//call button.js
</script>

  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to SecureLock - beta 1.0</p>
    <form><input type='button' value='unLock' onclick='unLock()'/></form>
  </body>

</html>

Thank you.

1

There are 1 answers

0
mherzig On BEST ANSWER

In your unLock() JS function you do an AJAX call with the method POST, but in your route you have

router.get('/unLock', function(req, res){
    ...
});

so there is no route to respond to a POST. Change the line above to router.post or change the AJAX method to type GET. Probably the latter if you aren't actually sending any data to the server with the AJAX call.