如何使用grunt

时间:2015-05-04 21:03:30

标签: node.js express gruntjs

我使用express JS来创建反向代理。基本上,它会将任何API请求转发给python服务器。

我的问题是:how do I run the reverse proxy using grunt

反向代理如下所示:

   (function(){
    var express = require('express');
    var request = require('request');
    var path = require('path');
    var logger = require('morgan');
    var bodyParser = require('body-parser');

    ///////////////////////

    var app = express();
    var path_to_dist = 'path/to/where/static/files/are/kept';
    var TARGET_URL = 'http://localhost:8000/api'; 

    app.use(logger('dev'));
    app.use(forward(/\/api\/(.*)/, TARGET_URL));

    //forward should always be called BEFORE bodyParser, because bodyParser modifies request
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use('/enforce', express.static(path.join(__dirname, path_to_dist)));
    app.enable('trust proxy');

    // var server = app.listen(8080, function () {

    //  var host = server.address().address;
    //  var port = server.address().port;

    //  console.log('Listening on http://%s:%s', host, port);
    // });

    ///////////////////////////////////////////////////

    function forward(pattern, host){
      return function(req, res, next){
        if(req.url.match(pattern)){
          var service_path = req.url.match(pattern)[1]
            , service_url = [host, service_path].join('/');
          console.log(req.method.toLowerCase());
          req.pipe(request[req.method.toLowerCase()](service_url)).pipe(res);
        }else{
          next();
        }
      }
    }
})();

我正在尝试使用grunt-express,但不起作用。永远需要咕噜声才能开始,并在几分钟之后崩溃: 像这样:

 grunt.initConfig({
  express: {
    custom: {
      options: {
        port: 8080,
        bases: 'www-root',
        server: path.resolve('./express-server/app')
      }
    }
  }
});

0 个答案:

没有答案