使用node.js将url显示到控制台中

时间:2016-04-05 13:26:53

标签: node.js

如何将request.url显示到console.log中,看看有人打过哪个网址? 所以我想看一下每个电话的控制台与网址一行。

抱歉,我是node.js的初学者;-) 谢谢你的帮助。

//Lets require/import the HTTP module
var http = require('http');

//Lets define a port we want to listen to
const PORT=8083; 

//We need a function which handles requests and send response
function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
    console.log("URL:%s");
});

1 个答案:

答案 0 :(得分:3)

如果您使用快递,则可以设置自定义中间件,每次请求都会调用该中间件。



// logging middleware
var num = 0;
app.use(function (req, res, next) {
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    var method = req.method;
    var url = req.url;

    console.log((++num) + ". IP " + ip + " " + method + " " + url);
    next();
});




但您可以对您的http模块服务器执行相同的操作:)