我正在NodeJS中构建一个家庭自动化系统,我想使用Express中的Shell_exec函数在我的覆盆子pi上发出一些命令。如何在JADE中使用onclick事件执行此操作?
这是我在Express中的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 shell_exec = require('shell_exec').shell_exec;
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/homeapp');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public/images', 'favicon-32x32.png')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
var User = require('./models/User');
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
module.exports = app;
这是我的index.js与路由:
var express = require('express');
var router = express.Router();
var auth = require('../controller/AuthController.js');
router.get('/', auth.home);
router.get('/login', auth.login);
router.post('/login', auth.doLogin);
router.get('/logout', auth.logout);
module.exports = router;
这是我希望onclick事件执行命令的玉文件:
div.btn(onclick="shell_exec");
答案 0 :(得分:1)
您无法从Jade视图调用任何Node.js命令。在客户端的浏览器中解析Jade视图,您在该文件中编写的任何javascript命令都将在浏览器中执行。
在视图中,您应该添加将向node.js服务器创建AJAX请求的代码,并在node.js app中创建将处理请求并执行命令的路由(shell_exec)。