当我通过'?'

时间:2016-11-19 18:10:09

标签: node.js rest express curl

我有以下申请

var express = require('express')
var bodyParser = require('body-parser')

var app = express();

// All cals should be application/json parser
var jsonParser = bodyParser.json();
app.use(jsonParser);

const path=require('path')
var cwd=process.cwd()

/**
* Append Below here your api endpoints
*/

var Weather=require(path.resolve(cwd,'application/controllers/weather.js'));
var w=new Weather(app);

/**
* Do not append below here your api endpoints
*/

app.listen(8000, function () {
  console.log('Umbrelapp Backend app listening on port 8000!')
})

我还有以下控制器:

/**
* @param object express The basic express onject that handles the http request
*/
function Weather(express)
{
  var self=this;
  var endpoint='/weather';

  express.use(endpoint,function(req, res,next)
  {
    if(http.preprocess(req,res,next,endpoint))
    {
      switch (req.method) {
        case 'GET':
            self.get(req, res,next);
          break;
        default:
        self.unsupportedAction(req,res,next);
      }
    }
  });

  /**
  * Handler for http Get method
  * @param req The http request
  * @param res The http response
  * @param next The next express.js http handler
  */
  self.get=function(req,res,next)
  {
    console.log(req.params);
    res.send('Hello');
  };

  /**
  * Default handler for unwanted http methods
  * @param req The http request
  * @param res The http response
  * @param next The next express.js http handler
  */
  self.unsupportedAction=function(req,res,next)
  {
    res.status(405);
    res.send('None shall pass');
  }
}

module.exports=Weather;

但是当我尝试以下curl命令时

  

curl -X GET -i http://localhost:8000/weather?hello=12;回声

我得到以下回复

HTTP/1.1 404 Not Found
X-Powered-By: Express
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 29
Date: Sat, 19 Nov 2016 18:06:41 GMT
Connection: keep-alive

Cannot GET /weather?hello=12

但是当我做下面的卷曲时:

  

curl -X GET -i http://localhost:8000/weather;回声

我得到以下回复

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 5
ETag: W/"5-ixqZU8RhEpaoJ6v4xHgE1w"
Date: Sat, 19 Nov 2016 18:08:08 GMT
Connection: keep-alive

Hello

您是否知道如何在任何给定的参数范围内完成curl命令的工作?

2 个答案:

答案 0 :(得分:0)

最后这对我有用:

  express.get(endpoint,function(req, res,next)
  {
    self.get(req, res,next);
  })
  .all(endpoint,function(req, res,next)
  {
    if(http.preprocess(req,res,next,endpoint))
    {
      switch (req.method) {
        //Just ot make sure thet get will execute all
        case http.method.GET:
            self.get(req, res,next);
          break;
        default:
        self.unsupportedAction(req,res,next);
      }
    }
  });

答案 1 :(得分:-1)

以下列形式定义端点对我有用:

var endpoint='/weather?*';