我写了一个函数,定期检查连接的Android设备的电池状态并返回一个数组。如何在服务器启动时运行此功能并使其在向其他页面提供信息的同时持续运行?
var minutes = 1, the_interval = minutes * 60 * 500;
setInterval(function() {
adb.devices().then(function(devices) {
var promises = new Array();
for (var i = 0; i < devices.length; i++){
promises.push(adb.checkBattery(devices[i]));
}
Promise.all(promises).then(function(availableDevices) {
console.log('Updated:');
console.log(availableDevices);
return availableDevices;
});
});
}, the_interval);
这是我打开新项目时自动创建的app.js文件。我为我创建的各种路线添加了app.use。
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var multer = require('multer');
var adb = require('./adb.js');
var index = require('./routes/index');
var users = require('./routes/users');
var devices = require('./routes/devices');
var openBrowser = require('./routes/openBrowser');
var closeBrowser = require('./routes/closeBrowser');
var openApp = require('./routes/openApp');
var closeApp = require('./routes/closeApp');
var install = require('./routes/install');
var uninstall = require('./routes/uninstall');
var pull = require('./routes/pull');
var push = require('./routes/push');
var battery = require('./routes/battery');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// 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);
app.use('/devices', devices);
app.use('/openBrowser', openBrowser);
app.use('/openApp', openApp);
app.use('/closeApp', closeApp);
app.use('/install', install);
app.use('/closeBrowser', closeBrowser);
app.use('/uninstall', uninstall);
app.use('/pull', pull);
app.use('/push', push);
app.use('/battery', battery);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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;
答案 0 :(得分:0)
使用express,如果我理解正确,你应该在某个时间间隔内运行它:
var express = require('express');
var app = express();
app.listen(3000, function () {
yourFunction();
setInterval(yourFunction, << period in ms >>);
});
function yourFunction()
{
<< your code >>
}
如果您想使用您的功能对Web服务器的某些请求做出反应,您最好使用中间件:
var express = require('express');
var app = express();
app.use(function(req,res) {
if(condition)
yourFunction();
})
app.listen(3000, function () {
});
function yourFunction()
{
<< your code >>
}
答案 1 :(得分:0)
screenshot根据documentation,您可以使用
server = app.listen(3000, function () {
});
server.on('listening', () => {
your function()
})