我想将客户端调试到 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();
});
所以,不清楚你的意思...但这应该有效(没有快递(:
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);