我是Nodejs和Expressjs的新手。我正在考虑让脚本在后台连续运行而不会中断应用程序。有许多调度NodeJs模块(如节点调度,后来等)可用,但我无法想象如何将它们包含在我的ExpressJs应用程序中。
除此之外,我应该包括这个模块;在应用程序级别或路由器上。
我希望环境不是问题,我在Windows 7 32位盒上运行这个应用程序。
我使用Yeoman Generator创建了ExpressJs App。从生成器生成的文件中复制和粘贴代码。
config.js
var path = require('path'),
rootPath = path.normalize(__dirname + '/..'),
env = process.env.NODE_ENV || 'development';
var config = {
development: {
root: rootPath,
app: {
name: 'nodejswebsocketapp'
},
port: process.env.PORT || 3000,
},
test: {
root: rootPath,
app: {
name: 'nodejswebsocketapp'
},
port: process.env.PORT || 3000,
},
production: {
root: rootPath,
app: {
name: 'nodejswebsocketapp'
},
port: process.env.PORT || 3000,
}
};
module.exports = config[env];
express.js
var express = require('express');
var glob = require('glob');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compress = require('compression');
var methodOverride = require('method-override');
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
next();
}
module.exports = function(app, config) {
var env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');
// app.use(favicon(config.root + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(compress());
app.use(express.static(config.root + '/public'));
app.use(methodOverride());
app.use(allowCrossDomain);
var controllers = glob.sync(config.root + '/app/controllers/*.js');
controllers.forEach(function (controller) {
require(controller)(app);
});
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
if(app.get('env') === 'development'){
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err,
title: 'error'
});
});
}
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {},
title: 'error'
});
});
};
app.js
var express = require('express');
var config = require('./config/config');
var app = express();
var expressWs = require('express-ws')(app);
require('./config/express')(app, config);
app.listen(config.port, function () {
console.log('Express server listening on port ' + config.port);
});
在此之后我创建了一个控制器文件 的 scheduleTaskController.js
var express = require('express'),
router = express.Router(),
schedule = require('node-schedule');
module.exports = function (app) {
app.use('/', router);
app.use('/schedule', router);
};
router.get('/schedule', function (req, res, next) {
console.log(schedule.RecurrenceRule());
var rule = new schedule.RecurrenceRule();
rule.second = 30;
schedule.scheduleJob(rule, function(){
console.log(new Date(), 'The 30th second of the minute.');
});
});
点击网址'/ schedule'会产生任何结果。不在浏览器中,也不在命令提示符中,我期待console.log的结果。
答案 0 :(得分:0)
我会创建一个脚本文件夹,然后从server.js
文件中请求该文件。