Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从url获取日期并返回json_Node.js_Centos - Fatal编程技术网

Node.js 从url获取日期并返回json

Node.js 从url获取日期并返回json,node.js,centos,Node.js,Centos,使用这个简单的例子 var server = http.createServer(function(req, res){ var output = {message: "Hello World!"}; var body = JSON.stringify(output); res.writeHead(200, { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" });

使用这个简单的例子

var server = http.createServer(function(req, res){
  var output = {message: "Hello World!"};
  var body = JSON.stringify(output);

  res.writeHead(200, {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*"
  });
  res.end(body);
});
我可以返回json对象。我怎样才能从这样的请求中返回一些参数

url: http://localhost:8080/get/jhon
output: {"message": "Hello jhon"}

您可以修改.htaccess文件以将URL解析为参数。有关示例,请参见此问题:

您可以修改.htaccess文件以将URL解析为参数。查看此问题以获取示例:

请求添加一个检查程序。url

基本上,以您自己的代码为例:

var server = http.createServer(function(req, res){
  var output = {message: "Hello World!"};
  var body = JSON.stringify(output);
  res.writeHead(200, {
     "Content-Type": "application/json",
     "Access-Control-Allow-Origin": "*"      
  });
  //.split creates an array with strings from the given url
  //req.url -> gives everything that's right after the http://localhost:8080/ portion
  var requested = req.url.split("/");
  output.message = "Hello " + requested[2]; // 0 is empty, 1 is "get"
  res.end(output);
});

希望有帮助。

请求添加一个检查程序。url

基本上,以您自己的代码为例:

var server = http.createServer(function(req, res){
  var output = {message: "Hello World!"};
  var body = JSON.stringify(output);
  res.writeHead(200, {
     "Content-Type": "application/json",
     "Access-Control-Allow-Origin": "*"      
  });
  //.split creates an array with strings from the given url
  //req.url -> gives everything that's right after the http://localhost:8080/ portion
  var requested = req.url.split("/");
  output.message = "Hello " + requested[2]; // 0 is empty, 1 is "get"
  res.end(output);
});
希望有帮助。

找到更好的方式require('url')。parse(req.url)找到更好的方式require('url')。parse(req.url)