[英]Using child_process.exec to send a command returning 404
I have a simple server setup using NodeJs, from which I would like to send a command to the terminal on the server. 我有一个使用NodeJ的简单服务器设置,我想从中将命令发送到服务器上的终端。
I'm trying to use child_process.exec to send a command, which when I run from the terminal works correctly. 我正在尝试使用child_process.exec发送命令,当我从终端运行时该命令可以正常工作。
The issue I'm having is that the POST method /unLock is not found. 我遇到的问题是找不到POST方法/ unLock。 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. 我已经在下面包含了服务器和路由/视图的代码,我是node的新手,正尝试学习,以便对您有所帮助。
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;
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;
<!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. 谢谢。
In your unLock()
JS function you do an AJAX call with the method POST
, but in your route you have 在您的unLock()
JS函数中,您可以使用POST
方法进行AJAX调用,但是在您的路由中
router.get('/unLock', function(req, res){
...
});
so there is no route to respond to a POST
. 因此没有办法响应POST
。 Change the line above to router.post
or change the AJAX method to type GET
. 将上面的行更改为router.post
或将AJAX方法更改为键入GET
。 Probably the latter if you aren't actually sending any data to the server with the AJAX call. 如果您实际上没有通过AJAX调用将任何数据发送到服务器,则可能是后者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.