[英]How execute a javascript in a node.js server
I have written a node.js server. 我已经编写了一个node.js服务器。
I am able to extract the HTTP POST and GET variable from the request. 我能够从请求中提取HTTP POST和GET变量。
I would like to pass those variable to the js script on the server to be executed. 我想将那些变量传递给要执行的服务器上的js脚本。
In PHP to execute a script I would just point to it @ www.example.com/path/file.php?variable=value 在PHP中执行脚本,我只想指向它@ www.example.com/path/file.php?variable=value
<?php echo "You have ".$_GET['variable'];?>
I want to achieve the same with noge.js @ www.example.com/path/file.njs?variable=value 我想通过noge.js @ www.example.com/path/file.njs?variable=value实现相同
My problem is the file.njs is executed as a text. 我的问题是file.njs作为文本执行。 I am not using express, a solution without it would be appreciated.
我没有使用快递,没有它的解决方案将不胜感激。
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 );
}
Lets say you have a URL parameter named variable
. 假设您有一个名为
variable
的URL参数。 I think this would work: 我认为这会起作用:
var parameters = url.parse(request.url, true);
var variable = parameters.variable;
I haven't used node.js in a while, but I'm pretty sure this works. 我已经有一段时间没有使用node.js了,但是我很确定这是可行的。
I don't know what your code chunk is supposed to do, but here is a basic Hello World for you that prints out a get parameter: 我不知道您的代码块应该做什么,但是这里有一个基本的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);
Now just visit 127.0.0.1:8000/?variable=foo
现在只需访问
127.0.0.1:8000/?variable=foo
In summary what I wanted to achieve is use node.js like PHP. 总之,我想实现的是使用像PHP这样的node.js。
to execute a PHP file like 执行像
www.example.com/path/file.php?variable=value www.example.com/path/file.php?variable=value
With node.js 使用node.js
www.example.com/path/file.js?variable=value www.example.com/path/file.js?variable=value
The solution I have come up with is to make the requested javascript into a module and include it with the function require
. 我想出的解决方案是将请求的javascript放入模块中,并将其包含在
require
函数中。
eg 例如
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
});
Though not thoroughly tested this solution works for me and I can use it even for dynamic pages. 尽管尚未经过全面测试,但该解决方案对我来说仍然有效,即使是动态页面也可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.