简体   繁体   English

我如何使用:作为拆分器并创建2个单独的字符串变量来拆分node.js函数中的querydata

[英]how can i split querydata in node.js function using : as the splitter and creat 2 seprate string variable

okay so here is my node.js script 好的,这是我的node.js脚本

  // Configure our HTTP server to respond with Hello World to all requests.
  var server = http.createServer(function (request, response) {
  var queryData = url.parse(request.url, true).query;
  response.writeHead(200, {"Content-Type": "text/plain"});

  if (queryData.name) {
    // user told us their name in the GET request, ex: http://host:8000/?name=Tom
    var exec = require('child_process').exec;

    exec ("casperjs test.js " + queryData.name + '\n',function(err, stdout, stderr) {

        response.end(stdout);

    });

  } else {
    response.end("Contact Admin - Not Working\n");
  }
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8283);

basically I want to be able to split querydata.name into to seprate variable to use in the exec 基本上我希望能够将querydata.name拆分为单独的变量以在exec中使用

here is an example of what I want to do 这是我想做的一个例子

split querydata.name into 
value1
and
value2

so I can use them like this 所以我可以这样使用它们

exec ("casperjs test.js " + value1 + " " + value2 + '\n',function(err, stdout, stderr)

can anyone help me achieve this the string needs splitting using : as the splitter 谁能帮我实现这一点,字符串需要使用:作为分隔符进行分割

----------------------EDIT CODE I HAVE TRIED this now works edited again ---------------------------------



var server = http.createServer(function (request, response) {
  var queryData = url.parse(request.url, true).query;
  response.writeHead(200, {"Content-Type": "text/plain"});

  if (queryData.name) {
    // user told us their name in the GET request, ex: http://host:8000/?name=Tom
    var basevalue = queryData.name;
    var value = basevalue.split (":");
    var exec = require('child_process').exec;

    exec ("casperjs test.js " + value[0] + " " + value[1] + '\n',function(err, stdout, stderr) {

        response.end(stdout);

    });

  } else {
    response.end("Contact Admin - Not Working\n");
  }
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8283);

and the error I get 和我得到的错误

[root@cyber-hosted ~]# node no1.js

/root/no1.js:1
on (exports, require, module, __filename, __dirname) { var server = http.creat
                                                                ^
ReferenceError: http is not defined
at Object.<anonymous> (/root/no1.js:1:76)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
[root@cyber-hosted ~]#

I just cannot get the split working correctly 我只是无法正常工作

Your problem isn't with the split. 您的问题不在于拆分。 The stacktrace you posted states ReferenceError: http is not defined . 您发布的stacktrace指出ReferenceError: http is not defined

This is because you need to require any modules you use in your code. 这是因为您需要在代码中使用的任何模块。

If you add the line var http = require('http') to the top of your file, it should fix the problem. 如果将var http = require('http')行添加到文件顶部,则应该可以解决该问题。

edit: You'll need to add var url = require('url') as well. 编辑:您还需要添加var url = require('url')

edit2: Tested this code, it should work: edit2:测试了此代码,它应该可以工作:

var http = require('http')
var url = require('url')

var server = http.createServer(function (request, response) {
  var queryData = url.parse(request.url, true).query;
  response.writeHead(200, {"Content-Type": "text/plain"});

  if (queryData.name) {
    // user told us their name in the GET request, ex: http://host:8283/?name=Tom

    var exec = require('child_process').exec;

    exec ("casperjs test.js " + queryData.name + '\n',function(err, stdout, stderr) {

        response.end(stdout);

    });

  } else {
    response.end("Contact Admin - Not Working\n");
  }
});

server.listen(8283);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM