Giter Site home page Giter Site logo

nginx-nodejs-module's Introduction

nginx-nodejs-module

Run node (v8) scripts directly inside nginx!

Current Project Status

The project is in the very early stages of development. Currently, running a simple script inside a nginx location block and returning a primitive just about works (and is pleasingly very fast). There is no support yet for outgoing HTTP requests.

If you're better than me at C/C++ and want to contribute, certainly do open a pull request.

Example Configuration

load_module /usr/local/nginx/lib/ngx_http_nodejs_module.so;
events {}

daemon off;
http {
	server {
		listen 127.0.0.1:8080;

		location /hello {
			nodejs_block {
				res.setHeader('content-type', 'text/html')
				return 1E6 * Math.random() | 0 
			}
		}

		location /echo_body {
			nodejs_block {
				return function (req, res, next) {
					let data = [];

					req.on('data, function (result) {
						data.push(result);
					})

					req.on('end, function () {
						res.end(Buffer.concat(data));
					})
				}
			}
		}

		location /file {
			nodejs_allow_require on;
			nodejs_block {
				const fs = require('fs')

				return async function (req, res, next) {
					res.setHeader('content-type', 'text/html')
					res.end(await fs.promises.readFile('/tmp/test.html'))
				}
			}
		}

		location /world {
			nodejs_block {
				return res.json(req)
			}
		}

		location /async {
			nodejs_block {
				return async function (req, res, next) {
					setTimeout(
						_ => res.json(req),
						100
					)
				}
			}
		}

		location /external {
			nodejs_allow_require on;
			nodejs_block {
				const crypto = require('crypto');
				return function (req, res) {
					res.end(crypto.createHash('md5').update(req.connection.remoteAddress).digest('hex'))
				}
			}
		}
	}
}

Building

The module can be built as a dynamic module using the nginx build system, but you will need to fetch some dependencies if you don't have libnode on your system (nvm doesn't include it). If node couldn't be found, it will be compiled during the configuration step. V8 will take some time to compile, so grab a cup of coffee (or three).

For Ubuntu, the easiest (and recommended) way to install libnode is to use this third party PPA and run apt-get install libnode108 libnode-dev.

cd nginx-1.25.1
./configure --add-dynamic-module=../nginx-nodejs-module [...]
make && make install

You can also build the included Dockerfile and run like so

docker run -v ./examples/nginx.conf:/etc/nginx/nginx.conf -p 8080:8080 nginx-with-nodejs 

Motivation

I thought it would be fun... Hopefully I can learn C++ along the way.

nginx-nodejs-module's People

Contributors

jamie0 avatar

Stargazers

Mohamad Ibrahim avatar lanre avatar Andy Buckingham avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.