webServer.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var express = require('express');
  3. var Queue = require('../');
  4. var q = Queue.Q;
  5. var app = express();
  6. var port = 8800;
  7. var queue = new Queue(1,{
  8. 'workAdd' : setBusy
  9. ,'workFinally': setBusy
  10. })
  11. var isBusy = false
  12. function setBusy(){
  13. var queueLength = this.getLength();
  14. isBusy = queueLength > 3;
  15. }
  16. function promfun1(i){
  17. var deferred = q.defer();
  18. setTimeout(function(){
  19. deferred.resolve(i)
  20. },sp)
  21. return deferred.promise;
  22. }
  23. app.use('/',function(req, res, next){
  24. if(isBusy){
  25. return next("server busy!")
  26. }else{
  27. next();
  28. };
  29. })
  30. app.all('/', function(req, res, next){
  31. queue.go(promfun1,['Hello Word!']).done(function(data){
  32. res.end(data)
  33. },function(err){
  34. next(new Error(err));
  35. })
  36. });
  37. app.use(function (err, req, res, next) {
  38. console.error('[' + new Date() + ']\n' + err.stack)
  39. var msg = err.stack || err.toString();
  40. res.status(500).send(msg || "<h1>出错了!</h1> 请刷新页面或联系管理员!")
  41. })
  42. app.listen(port, function() {
  43. console.log(new Date() + ':start server on port ' + port)
  44. })