简体   繁体   English

Node.js Express app.get()在cron中不起作用

[英]Node.js Express app.get() doesn't work in cron

I want to make a 10-minutes cron, which is executing some actions, but at first, I've got a problem with how to simply call an action. 我想做一个10分钟的cron,它正在执行一些动作,但是起初,我对如何简单地调用一个动作有疑问。 I'm using MEAN.js framework. 我正在使用MEAN.js框架。 My approach is following: 我的方法如下:


server.js, at the bottom server.js,在底部

new CronJob('* */10 * * * *', function() {
    console.log("CronJob executed");
    app.get('/cronBarbarianAttack', function(req, res) {
    });
}, null, true);

cron.server.routes.js cron.server.routes.js

'use strict';

module.exports = function(app) {
    var cron = require('../../app/controllers/cron.server.controller');

    // Cron Routes
    app.route('/cronBarbarianAttack').get(cron.barbarianAttack);

};

cron.server.controller.js cron.server.controller.js

'use strict';

var mongoose = require('mongoose'),
    errorHandler = require('./errors.server.controller'),
    Game = mongoose.model('Game'),
    _ = require('lodash');

exports.barbarianAttack = function(req, res) { 
    console.log("executed");
    res.json(null);
};

But this doesn't call the controller function. 但这不会调用控制器功能。 It only prints to console "CronJob Executed", without second "executed" message. 它仅打印到控制台“ CronJob已执行”,而没有第二条“已执行”消息。 Where is the problem and what's wrong with my approach? 问题出在哪里,我的方法出了什么问题? Thanks :) 谢谢 :)

http://expressjs.com/4x/api.html#app.get http://expressjs.com/4x/api.html#app.get

app.get() only routes your HTTP GET requests to specified callback. app.get()仅将您的HTTP GET请求路由到指定的回调。

There is no HTTP request, so nothing was called. 没有HTTP请求,因此未调用任何内容。

You do not need express framework to pass callback to cronjob. 您不需要快速框架即可将回调传递给cronjob。

Assume you use node-cron , so check the docs to this project to proper write cron jobs 假设您使用node-cron ,那么请检查该项目的文档以正确编写cron作业

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM