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 );
}
요약하자면 내가 원하는 것은 PHP와 같은 node.js를 사용하는 것입니다. www.example.com/path/file.php?variable=value 와
같은 PHP 파일을 실행하려면 node.js로 www.example.com/path/file.js?variable=value 내가 생각 해낸 해결책은 요청 된 자바 스크립트를 모듈에 넣고 함수와 함께 포함합니다 . 예 :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
});
철저히 테스트하지는 않았지만이 솔루션은 저에게 효과적이며 동적 페이지에도 사용할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다