2016-11-19 4 views
0

私はfollwingアプリケーションExpress.js 404で '?'

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コマンド

カール-X -i http://localhost:8000/weather?hello=12をGETしてみたときに、私は次の応答に

を取得エコー

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 

しかし、私は、次のカールを行うとき:

カール-X GET -i http://localhost:8000/weather

エコー私はあなたが私が与えられたパラメータのいずれかのammountでの作業の両方カールのコマンドを作ることができますどのように任意のアイデアを持っていますか、次の応答

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 

を取得しますか?次の形式でエンドポイントを定義

答えて

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

が私の作品:

var endpoint='/weather?*';