PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] how to use nodejs print orignal http message

 July 11, 2022     express, http, message, node.js, request     No comments   

Issue

I want to debug the client to nodejs server, print the http request original message like curl. any one can help me?

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();
});

Solution

So, not clear what you mean... but this should work (without express):

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);   

Or this one with express middleware:

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);   


Answered By - Sebastian Hildebrandt
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing