0

我有一個簡單的服務器設置使用NodeJs,我想從中發送命令到服務器上的終端。使用child_process.exec發送命令返回404

我試圖使用child_process.exec發送一個命令,當我從終端運行正常工作。

我遇到的問題是找不到POST方法/ unLock。作爲我得到的迴應。

POST /unLock 404 1.770 ms - 1186 

我已經包括服務器和路由碼/意見我在下面設置了,我是新來的節點,並努力學習,因此任何幫助,將不勝感激。

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> 

謝謝。

回答

1

在你unLock() JS工作,你做一個AJAX調用該方法POST,但在你的路線,你有

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

所以沒有對一個POST迴應路線。將上面的行更改爲router.post將AJAX方法更改爲GET。如果您實際上沒有通過AJAX調用向服務器發送任何數據,那麼後者可能就是後者。