从url获取date并返回json

使用简单的例子

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"} 

request.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); }); 

希望有所帮助。

您可以修改.htaccess文件来parsingURL作为参数。 看到这个问题的例子: htaccess url作为参数