我传递的信息在哪里? [英] Where did the information I passed in go?

查看:100
本文介绍了我传递的信息在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个节点http服务器。

I have this node http server.

/**
 * Module dependencies.
 */

var http  = require('http'),
    fs    = require('fs'), 
    cycle = require('./cycle.js');

/**
 * Create the server.
 */

var server = http.createServer(function (req, resp) {
    console.log(JSON.decycle(req) );
    if ('GET' === req.method && '/images' === req.url.substr(0, 7) && 
            '.jpg' === req.url.substr(-4) ) {
        fs.stat(__dirname + req.url, function (err, stat) {
            if (err || !stat.isFile() ) {
                res.writeHead(404);
                res.end('Not Found');
                return;
            }
            serve(__dirname + req.url, 'application/jpg');
        });
    } else if ('GET' === req.method && '/' === req.url) {
        serve(__dirname + '/index.html', 'text/html');
    } else {
        resp.writeHead(404);
        resp.end('Not found');
    }

    function serve(path, type) {
        resp.writeHead(200, {'Content-Type': type});
        fs.createReadStream(path).pipe(resp);
    }
});

/**
 * Listen.
 */

server.listen(3000);

(它使用Crockford的对象打印工具。 https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

(It uses Crockford's object-printing utility. https://github.com/douglascrockford/JSON-js/blob/master/cycle.js)

当我点击此请求时,我传入的信息项不会出现在请求中。为什么不呢?

When I hit it with this request, the information items I passed in don't appear in the request. Why not?

RickH$ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -H "Postman-Token: 647062e3-bd38-edf0-3b3a-e56227daf94c" -d '&first_name=James&last_name=Taylor&city=Urbana&state=Illinois' "localhost:3000"
RickH$

这是一个较大项目的简化版本。在较大的项目中,请求被传送到另一个服务器,该服务器根据请求的内容做出反应。所以我觉得我知道内容已经过去了。但它在哪里?

This is a boiled down version of a larger project. In the larger project, the request is piped to another server, which reacts based on the contents of the request. So I feel that I know the content is getting through. But where is it?

稍后----------------

Later----------------

/*********
Version 2 of server callback function.  This one shows the full request body.
var server = http.createServer(function (req, resp) {
    console.log();
    console.log('=========================%==========================');
    console.log();
    console.log(JSON.decycle(req) );

    req.rawBody = '';
    req.setEncoding('utf8');

    req.on('data', function (chunk) {
        req.rawBody += chunk;
        // next();
    });

    req.on('end', function () {
        console.log();
        console.log('~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~');
        console.log();
        console.log(JSON.decycle(req) );
        // next();
    });

    if ('GET' === req.method && '/images' === req.url.substr(0, 7) && 
            '.jpg' === req.url.substr(-4) ) {
        fs.stat(__dirname + req.url, function (err, stat) {
            if (err || !stat.isFile() ) {
                res.writeHead(404);
                res.end('Not Found');
                return;
            }
            serve(__dirname + req.url, 'application/jpg');
        });
    } else if ('GET' === req.method && '/' === req.url) {
        serve(__dirname + '/index.html', 'text/html');
    } else {
        resp.writeHead(404);
        resp.end('Not found');
    }

    function serve(path, type) {
        resp.writeHead(200, {'Content-Type': type});
        fs.createReadStream(path).pipe(resp);
    }
});
*********/


推荐答案

您需要以下代码才能获取请求的正文(您的信息)

You need following code to get body(your information) of request

var data;
req.on('data', function(chunk){
    data += chunk;
});
req.on('end', function(){
     console.log(data);
});

这篇关于我传递的信息在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆