2016-09-19 9 views
-3

express.jsを使用しているnode.jsアプリケーションはhttp://bartonlp.org:8080です。私は私のログ出力中に、例えば、これを参照してください。Node.jsはURLを404の代わりに200として報告します

"GET http://www.baidu.com/?rnd=19712180733831423955225532 HTTP/1.0" 200 2749 
www.baidu.comが実際に答えてしまったために、私は他のすべての要求は404エラーではなく、これは、要求のように見える200を生成すると予想しているでしょう

どうしてですか?私のapp.get( '/' ...はメインページです)の唯一のもの

ここには非常に最小のnode.jsとexpress.jsのapp.jsとroute/index.jsがあります。 これは、元のファイルと同じ結果が得られます。ここに私のログファイルからのコピーである:

::ffff:94.102.49.174 - - [22/Sep/2016:23:22:29 +0000] "GET http://www.baidu.com/cache/global/img/gs.gif HTTP/1.1" 404 975 "-" "Mozilla" 
::ffff:61.157.96.111 - - [22/Sep/2016:23:49:10 +0000] "GET http://www.baidu.com/?rnd=3342104796019809757566816 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.111 - - [22/Sep/2016:23:49:10 +0000] "GET http://50.117.47.67:43530/?rnd=2031422537804277052916912 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.111 - - [22/Sep/2016:23:49:11 +0000] "GET http://123.249.29.201:/?rnd=173003480482578803940608 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:31 +0000] "GET http://123.249.29.201:/?rands=_518421966013600083518500 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:31 +0000] "GET http://www.baidu.com/?rands=_14790662340951392923112 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:32 +0000] "GET http://50.117.47.15:28712/?rands=_71269649426275522095128 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:33 +0000] "GET http://50.117.86.106/?rands=_146883056102225940108720 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:62.210.162.182 - - [23/Sep/2016:00:04:23 +0000] "GET/HTTP/1.1" 200 197 "-" "Ruby" 
::ffff:62.210.162.182 - - [23/Sep/2016:00:04:29 +0000] "GET/HTTP/1.1" 200 197 "-" "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" 
::ffff:62.210.162.182 - - [23/Sep/2016:00:04:30 +0000] "GET /HNAP1/ HTTP/1.1" 404 975 "-" "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" 

下「/ HNAP1 /」私が何を期待され404、 しかし、これらのログの行で失敗します。すべての200を得る:ここで

::ffff:61.157.96.111 - - [22/Sep/2016:23:49:10 +0000] "GET http://www.baidu.com/?rnd=3342104796019809757566816 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.111 - - [22/Sep/2016:23:49:10 +0000] "GET http://50.117.47.67:43530/?rnd=2031422537804277052916912 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.111 - - [22/Sep/2016:23:49:11 +0000] "GET http://123.249.29.201:/?rnd=173003480482578803940608 HTTP/1.0" 200 197 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;Windows NT 5.1;Windows NT 5.3; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:31 +0000] "GET http://123.249.29.201:/?rands=_518421966013600083518500 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:31 +0000] "GET http://www.baidu.com/?rands=_14790662340951392923112 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:32 +0000] "GET http://50.117.47.15:28712/?rands=_71269649426275522095128 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 
::ffff:61.157.96.112 - - [22/Sep/2016:23:56:33 +0000] "GET http://50.117.86.106/?rands=_146883056102225940108720 HTTP/1.0" 200 197 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" 

は私app.jsです:

ここでは
var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var routes = require('./routes/index'); 
var app = express(); 
// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 
// uncomment after placing your favicon in /public 
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
app.use(logger('combined')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 
app.use('/', routes); 
// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 
// error handlers 
// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: err 
    }); 
    }); 
} 
// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 
module.exports = app; 

binに/ WWWのスクリプトは

#!/usr/bin/env node 
/** 
* Module dependencies. 
*/ 
var app = require('../app'); 
var debug = require('debug')('newnode:server'); 
var http = require('http'); 
/** 
* Get port from environment and store in Express. 
*/ 
var port = normalizePort(process.env.PORT || '3000'); 
console.log("PORT: ", port); 
app.set('port', port); 
/** 
* Create HTTP server. 
*/ 
var server = http.createServer(app); 
/** 
* Listen on provided port, on all network interfaces. 
*/ 
server.listen(port); 
server.on('error', onError); 
server.on('listening', onListening); 
/** 
* Normalize a port into a number, string, or false. 
*/ 
function normalizePort(val) { 
    var port = parseInt(val, 10); 
    if (isNaN(port)) { 
    // named pipe 
    return val; 
    } 
    if (port >= 0) { 
    // port number 
    return port; 
    } 
    return false; 
} 
/** 
* Event listener for HTTP server "error" event. 
*/ 
function onError(error) { 
    if (error.syscall !== 'listen') { 
    throw error; 
    } 
    var bind = typeof port === 'string' 
    ? 'Pipe ' + port 
    : 'Port ' + port; 
    // handle specific listen errors with friendly messages 
    switch (error.code) { 
    case 'EACCES': 
     console.error(bind + ' requires elevated privileges'); 
     process.exit(1); 
     break; 
    case 'EADDRINUSE': 
     console.error(bind + ' is already in use'); 
     process.exit(1); 
     break; 
    default: 
     throw error; 
    } 
} 

/** 
* Event listener for HTTP server "listening" event. 
*/ 
function onListening() { 
    var addr = server.address(); 
    var bind = typeof addr === 'string' 
    ? 'pipe ' + addr 
    : 'port ' + addr.port; 
    console.log('Listening on ' + bind); 
} 

です。この環境は '急行newnode' を実行した後でここに私のルート/ index.js

var express = require('express'); 
var router = express.Router(); 
/* GET home page. */ 
router.get('/', function(req, res, next) { 
    res.render('index', { title: 'My Test Node App' }); 
}); 
module.exports = router; 

とあります。

これが十分に説明されることを願っています。

もう一度私の質問がなぜ 'http://www.baidu.com ...'レコードで404の代わりに200を得るのですか?

+2

私たちはここに電子メールでの回答を送信しません。このサイトはここでの質問と回答です。回答を探すためにサイトに戻ってきたくない場合は、使用する別のサイトを見つける必要があります。 – jfriend00

+0

質問に答えたときにStackoverflowで電子メールを送信できないのはなぜですか?他の多くのサイトがそうしています。 – bartonlp

+0

@ user1807480できます。 [あなたのユーザーの環境設定](http://stackoverflow.com/users/preferences/1807480)に移動し、[未読の受信トレイメッセージを私に電子メールで送信]をチェックします。 – NobodyNada

答えて

1

これは「不可能なURL」ではありません。完全に有効ですが、クライアントがプロキシサーバー経由で訪問したいサイトが完全なURLであるHTTPプロキシの場合、通常はパス(/foo)の代わりに完全なURL(例:http://example.org/foo)が使用されます。

編集:最近投稿されたサーバー側のルーティングコードを確認した後、ルーティング設定に間違いはありません。代わりに、it only inspects the path portion of a request url(他の解析された部分は無視します)のバグがあります。私は問題hereを提出しました。

特定のケースでは、完全なURL(パスがない)を要求しているクライアントは現在、/ルートハンドラから応答を受けています。他の完全なURL(http://www.baidu.com/cache/global/img/gs.gifなど)は、./public/cache/global/img/gs.gifがなく、/cache/global/img/gs.gifの明示的なルートハンドラがないため、一致しません。これらの場合、サーバー側のログに404が正しく表示されます。

EDIT 2:あなたは、ルートハンドラのそれぞれにreq.originalUrlおよび/または同様の特性を確認する必要がありますどちらかは、驚くべきことに、これはExpressのメンテナによってバグとはみなされない、のでまたはは前に(一般的なミドルウェア・ハンドラを含めます他のリクエストハンドラ、おそらくロギングミドルウェアを除く)は、これらの完全なURLをチェックし、これらの種類の完全なURLリクエストを拒否する場合は適切に処理します。

は、ここでは、あなたのミドルウェア/ルートハンドラのスタックに最初を追加する例ミドルウェアです:

app.use(function(req, res, next) { 
    if (req.originalUrl.charCodeAt(0) !== 47/*'/'*/) 
    return res.sendStatus(400); 
    next(); 
}); 
+0

私が意味していたことは、私のノードサーバーが/に答えるだけでよいとは思えませんでした。これらのリクエストは/ではなく、404を受け取ったはずだと思っていたのですが、代わりに200が返ってきました。 – bartonlp

+0

あなたのルートによって異なります。それらを見ずに、理由を言うのは難しいです。 – mscdex

+0

私は自分のアプリを単純化しようとしますが、それでも起これば私はapp.jsとindex.jsファイルを投稿します。 – bartonlp

関連する問題