npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nginxc

v1.0.0

Published

Tiny framework for building `nginx.conf` files programmatically.

Downloads

5

Readme

nginxc

A Tiny framework for building nginx.conf files programmatically.

Install

npm i nginxc --save

Usage

see full example in ./example.js

const nginxc = require('nginxc');
const nc = nginxc();

nc.clause('server', cl => {
  cl
  .directive('listen', '80')
  .directive('server_name', 'example.com')
  .directive('client_max_body_size', '50M')
  .directive('root', '/home/ascari/example')
  .location('/', cl => {
    cl
    .directive('index', 'index.html')
    .directive('error_page', '400 402 403 404 /404.html')
    .directive('error_page', '500 502 504 /500.html')
    .directive('error_page', '503 /503.html')
    .directive('rewrite', '/dashboard /index.html break')
    .location('/about', cl => {
      cl.directive('try_files', '/About.html =404');
    })
    .location('/search', cl => {
      cl.directive('try_files', '/Search.html =404')
    })
  });
});

console.log(nc.toString());

Let's break it down

1) Require the nginxc module

const nginxc = require('nginxc');

2) Create a instance of NginxConfig, a class that represents a nginx.conf file.

const nc = nginxc();

3) We are creating a virtual domain for the sites-available/ directory, so we create a server clause. The callback is required and is executed synchronously, it will expose a server Clause instance as the first argument.

nc.clause('server', cl => {

4) We configure the server clause by adding directives that specify the port and hostname as well as the root folder. A Directive takes only 2 arguments, the first is the name of the directive and the second is its value.
  cl
  .directive('listen', '80')
  .directive('server_name', 'example.com')
  .directive('client_max_body_size', '50M')
  .directive('root', '/home/ascari/example')
5) Next, we create a Location clause at the root path. Here we configure the endpoint as well as add more Location clauses.
  .location('/', cl => {
    cl
    .directive('index', 'index.html')
    .directive('error_page', '400 402 403 404 /404.html')
    .directive('error_page', '500 502 504 /500.html')
    .directive('error_page', '503 /503.html')
    .directive('rewrite', '/dashboard /index.html break')
    .location('/about', cl => {
      cl.directive('try_files', '/About.html =404');
    })
    .location('/search', cl => {
      cl.directive('try_files', '/Search.html =404')
    })
  });
5) Finally, call toString() to see return the nginx configuration.

console.log(nc.toString());

outputs

server {
  listen 80;
  server_name example.com;
  client_max_body_size 50M;
  root /home/ascari/example;

  location / {
    index index.html;
    error_page 400 402 403 404 /404.html;
    error_page 500 502 504 /500.html;
    error_page 503 /503.html;
    rewrite /dashboard /index.html break;
    location /about {
      try_files /About.html =404;
    }
    location /search {
      try_files /Search.html =404;
    }
  }
}

API

class Directive A instance is returned when calling directive() or dir() on a Clause.

  • getter name -> {String}
  • getter value -> {String}
  • method toString() -> {String}

class Clause A instance is returned when calling clause() or cl() on a Clause.

  • getter name -> {String}
  • getter depth -> {Number}
  • method clause(name, callback) -> {Clause}
  • method directive(name, value) -> {Clause}
  • method location(path, callback) -> {Clause}
  • method cl(name, callback) -> {Clause}
  • method dir(name, value) -> {Clause}
  • method loc(path, callback) -> {Clause}
  • method toString() -> {Clause}

class Location extends Clause A instance is returned when calling location() or loc() on a Clause.

  • method toString() -> {String}

class NginxConfig extends Clause A instance is returned when calling nginxc()

Alias

You can use shorthand methods instead, they are.

| Method | Alias | |-----------|:------:| | directive | dir | | clause | cl | | location | loc |

License

MIT