как использовать сообщение для печати nodejs http - PullRequest
0 голосов
/ 01 июля 2018

Я хочу отладить клиент на сервере nodejs, напечатать оригинальное сообщение http-запроса, например, curl. кто-нибудь может мне помочь?

curl -vs -o /dev/null http://127.0.0.1:8080

var app = require('http').createServer(function (request, response) {
        // print http orignal message like curl

  });
app.listen(3000);   


//express 
var app=express();
app.use(function(req,res,next){ // print http orignal message like curl
    console.log(req.url)
    console.log(req.headers)
    console.log(req.query)
    console.log(req.body)
    next();
});

1 Ответ

0 голосов
/ 01 июля 2018

Итак, непонятно, что вы имеете в виду ... но это должно работать (без экспресса):

var app = require('http').createServer(function (request, response) {
    // print http orignal message like curl

    // request.method, URL and httpVersion
    console.log(request.method + ' ' + request.url + ' HTTP/' + request.httpVersion);

    // request.headers
    for (var property in request.headers) {
        if (request.headers.hasOwnProperty(property)) {
            console.log(property + ': ' + request.headers[property])
        }
    }
});
app.listen(3000);   

Или этот с экспресс-промежуточным программным обеспечением:

const express = require('express')

const app = express();

// middleware to track request message
app.use(function (req, res, next) {

    console.log(req.method + ' ' + req.url + ' HTTP/' + req.httpVersion);
    for (var property in req.headers) {
        if (req.headers.hasOwnProperty(property)) {
            console.log(property + ': ' + req.headers[property])
        }
    }
    next();
});

// your routes
app.get('/', function (req, res, next) {
  res.send('Hello World');
});

app.listen(3000);   
...