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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.