2016-09-19 71 views
-3

我有一個Node.js的應用期運用在http://bartonlp.org:8080運行express.js。我看到這一點,例如,在我的日誌輸出:Node.js的報告網址爲200,而不是404

"GET http://www.baidu.com/?rnd=19712180733831423955225532 HTTP/1.0" 200 2749 

我本來期望所有其他的要求來生產一個404錯誤,而不是200。這看起來像請求www.baidu.com實際上得到回答。

這怎麼會發生?我在我的app.get唯一(「/」 ......是一個主要的頁面。

這是一個很小的node.js加express.js app.js和路由/ 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; 

這裏是我的路線/ 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; 

這裏是斌/ 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' 之後。

我希望這可以解釋不夠好。

我的問題又是爲什麼那些「http://www.baidu.com ...」記錄最終得到一個200而不是404嗎?

+2

這裏不通過電子郵件發送答案。這個網站是在這裏的問題和答案。如果您不想回到網站尋找答案,那麼您將需要找到一個不同的網站來使用。 – jfriend00

+0

爲什麼Stackoverflow在問題已回答時不能發送電子郵件?許多其他網站都這樣做。 – bartonlp

+0

@ user1807480它可以;請轉到[您的用戶首選項](http://stackoverflow.com/users/preferences/1807480)並選中「通過電子郵件將我的未讀收件箱郵件發送給我。」 – NobodyNada

回答

1

這不是一個「不可能的URL」。它完全有效,但完整的URL(例如http://example.org/foo)而不是路徑(/foo)通常用於HTTP代理,其中完整的URL是客戶端希望通過代理服務器訪問的站點。

編輯:在審查了最近發佈的服務器端路由代碼後,路由設置沒有錯誤。相反,它似乎有一個Express中的錯誤it only inspects the path portion of a request url(忽略任何其他解析的部分)。我提交了一個問題here

所以在您的特定情況下,那些要求一個完整的URL(即有沒有路徑)的客戶目前正在接受來自您的/路由處理的響應。其他完整網址(例如http://www.baidu.com/cache/global/img/gs.gif)不匹配,因爲您沒有./public/cache/global/img/gs.gif,也沒有明確的路由處理程序/cache/global/img/gs.gif)。對於那些在服務器端日誌中正確看到404的人。

編輯2:令人驚訝的,這是沒有考慮由快遞維護一個bug,所以你將不得不在每個路由執行的檢查req.originalUrl和/或類似性質包括通用中間件處理程序(之前任何其他請求處理程序(可能是日誌記錄中間件除外),如果您關心拒絕這些完整的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

相關問題