繁体   English   中英

如何在Node.js服务器中执行JavaScript

[英]How execute a javascript in a node.js server

我已经编写了一个node.js服务器。
我能够从请求中提取HTTP POST和GET变量。
我想将那些变量传递给要执行的服务器上的js脚本。
在PHP中执行脚本,我只想指向它@ www.example.com/path/file.php?variable=value

<?php echo "You have ".$_GET['variable'];?>

我想通过noge.js @ www.example.com/path/file.njs?variable=value实现相同
我的问题是file.njs作为文本执行。 我没有使用快递,没有它的解决方案将不胜感激。

 var sys=require("sys"),
 http=require("http"),
 path=require("path"),
 url=require("url"),
 filesys=require("fs"),
 mime=require("./node_modules/mime"),
 head=require("./node_modules/headers/initreq.njs");//this is used to extract the header
 http.createServer(handler).listen(80);//handler function is below
 sys.puts("Node Server Running on 80");

 function handler(request,response){
    var myPath=url.parse(request.url).pathname;//get the url
    var ext=myPath.replace(/(.)*\./,'');//get the extension
    var fullPath=path.join(process.cwd(),myPath);//get the working dir & join it with current working dir
    var mimeResult=mime.lookup(fullPath),acceptExt=['html','njs'];  

    if(acceptExt.indexOf(ext)!=-1){//only search HTTP header for html|njs files
        head.init(request,response,setContent);//head will correctly contain the $_GET AND $_POST variable
    } else {setContent();}  

    function setContent(){
        path.exists(fullPath,function(exists){
            if(!exists){
                response.writeHeader(404, {"Content-Type":"text/plain"});
                response.write("404 Not Found:: "+fullPath+"\n");
                response.end();
            }else{
                filesys.readFile(fullPath,"binary",function(err,file){
                    if(err){
                        response.writeHeader(500,{"Content-Type":"text/plain"});
                        response.write(err+"::"+myPath+"\n");
                        response.end();
                    }else{                      
                        response.setHeader("Content-Type", mimeResult);
                        response.writeHeader(200);
                        response.write(file,"binary");//this is the file that i want to execute and pass the $_POST & $_GET variable
                        response.end();
                    }
                });
            }
        });
    }
   sys.puts("Requested:: "+myPath.replace(/(.)*\//,'')+" - "+mimeResult );   
 }

假设您有一个名为variable的URL参数。 我认为这会起作用:

var parameters = url.parse(request.url, true);
var variable = parameters.variable;

我已经有一段时间没有使用node.js了,但是我很确定这是可行的。

我不知道您的代码块应该做什么,但是这里有一个基本的Hello World ,可以为您打印出一个get参数:

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

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

server.listen(8000);

现在只需访问127.0.0.1:8000/?variable=foo

总之,我想实现的是使用像PHP这样的node.js。
执行像
www.example.com/path/file.php?variable=value
使用node.js
www.example.com/path/file.js?variable=value
我想出的解决方案是将请求的javascript放入模块中,并将其包含在require函数中。
例如

http.createServer(function(req,res){
   var myPath=url.parse(req.url).pathname;//get the url
   var fullPath=path.join(process.cwd(),myPath);//get the working dir & join it with current working dir
    require(fullPath).content(req,res);//this is where the requested script is executed as a module. make sure to end the response (response.end()) in the module
});

尽管尚未经过全面测试,但该解决方案对我来说仍然有效,即使是动态页面也可以使用它。

暂无
暂无

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

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