项目作者: kathan

项目描述 :
Javascript CGI process manager.
高级语言: JavaScript
项目地址: git://github.com/kathan/js-cgi.git
创建时间: 2015-05-12T16:10:23Z
项目社区:https://github.com/kathan/js-cgi

开源协议:MIT License

下载


js-cgi - Javascript CGI process manager

js-cgi is a javascript CGI process manager, similar to php-fpm, for executing node.js scripts behind NGINX or Apache. Comes in handy if you want to run PHP along side node.js or if you don’t want to write your own web server into your node.js application.

Dependencies:

  • express.js

Configuration:

On startup, js-cgi will look for a config file called js-cgi.config in the same folder as the js-cgi.js file. If it’s not found it will use the defaults.

Example:

  1. module.exports = {
  2. port:3000,
  3. localhostOnly:true,
  4. workers:2
  5. };
  • port - Indicates which TCP port to listen on. default=3000
  • localhostOnly - Prevents remote agents from invoking scripts. default=true
  • workers - Number of worker processes. default=the number of processors on the local machine.

Usage:

Add a directive to your nginx.conf file. Using an “njs” extension on the server javascript files instead of a “js” extension will enable NGINX to distinguish server scripts from browser javascript files.

  1. location ~ [^/]\.njs(/|$) {
  2. proxy_pass http://localhost:3000;
  3. proxy_set_header X-Real-IP $remote_addr;
  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  5. proxy_set_header Host $http_host;
  6. proxy_set_header X-NginX-Proxy true;
  7. proxy_set_header path_translated $document_root$fastcgi_path_info;
  8. }

Middleware

If you want to use express.js middleware, create a “use.js” file and save it to the same folder as the js-cgi app like so:

  1. var bodyParser = require('body-parser'),
  2. cookieParser = require('cookie-parser'),
  3. fileUpload = require('express-fileupload');
  4. module.exports = function(app){
  5. app.use(fileUpload());//for uploading files
  6. app.use(bodyParser.json()); // for parsing application/json
  7. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  8. app.use(cookieParser());// for parsing cookies
  9. }

Once you configure and restart NGINX, you can start js-cgi.

  1. node js-cgi.js