簡體   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