2017-08-28 133 views
0

我建立在一個的NodeJS家庭自動化系統,我想火上使用快車了shell_exec功能我樹莓派一些命令。我怎樣才能在JADE onclick事件做到這一點?運行的Node.js了shell_exec命令的onclick

這是我在快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"); 

回答

1

您不能從Jade視圖調用任何Node.js命令。玉觀點在客戶端瀏覽器解析和任何你在該文件中寫JavaScript命令將在瀏覽器中執行。

在您應該添加代碼,這將創造AJAX請求到服務器的Node.js和Node.js的應用程序創建的路線,這將處理請求並執行命令(了shell_exec)的觀點。