所以,我有以下文件:
var sys = require('sys');
var mustache = require('mustache');
var view = {
title: "Joe",
calc: function() {
return 2 + 4;
}
};
var template = "{{title}} spends {{calc}}";
var html = mustache.to_html(template, view);
sys.puts(html);
当我使用 Node nodejs mypage.js
运行它时,我得到以下输出:
Joe spends 6
这一切都太棒了!我现在的问题是,我如何通过 Nginx 访问它?
例如http://example.com/mypage.js
编辑
这是 Nginx 配置(注意我使用 jsp
而不是 js
,因为 js
已被视为文件扩展名):
server{
listen 80 default_server;
server_name example.com;
root /usr/share/nginx/html/example.com;
location ~ \.jsp$ {
fastcgi_pass unix:/tmp/nginx.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
点击该页面会出现502 Bad Gateway
最佳答案
我知道这不是您所要求的,但这是在 Node 中执行此操作的最基本方法。如果你想构建一个 Node 服务器,首选标准是 express
var sys = require('sys');
var mustache = require('mustache');
function handleRequest(req, res){
var view = {
title: "Joe",
calc: function() {
return 2 + 4;
}
};
var template = "{{title}} spends {{calc}}";
var html = mustache.to_html(template, view);
res.setHeader('Content-Type', 'text/html');
res.end(html);
}
var http = require('http');
var server = http.createServer(handleRequest):
server.listen(3000, function(err){
console.log(err || 'Server listening on 3000');
})
关于javascript - 如何从 nginx 访问 Node 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29479513/