17
kiki
1y

TIL Nginx supports js out of the box. You don’t need nodejs to write servers in js. You can install Nginx and make it execute js code to generate pages. It even supports TypeScript!

https://nginx.org/en/docs/njs/
https://nginx.org/en/docs/...

Comments
  • 1
    JSSP 😁
  • 4
    WE ARE DOOMED!!! AAAAAAAAAAA
  • 1
    Can someone explain to me like I'm 5 why would I even want to do that?
  • 4
    @IHateForALiving

    1. To run JS code server-side without using node. You provision a server, install nginx, copy your code with scp or rsync. Boom, done.

    2. Because of how Nginx works, you will naturally achieve kinda-sorta zero-downtime deployment. The code is only executed when a request arrives. So if you change files in between requests, everything will carry on as normal. There is no Node server to restart.

    3. Job security
  • 3
    @IHateForALiving there are also downsides:

    1. Nginx calls it “njs” — a subset of JavaScript. granted, there are things like fetch()-like API and crypto, but it's otherwise incompatible with JS deps. Node deps won't work because you don't have Node, browser deps won't work because you don't have the browser. You're only limited to isomorphic packages — a very small subset of NPM library.

    2. You don't have imports. If you want deps, you have to bundle them somewhere, like they did for browsers back when import statements didn't work in the browser out of the box.

    3. There is no way to save state in between requests. None. You have to write it into a file or use an external storage.

    In short, it is in no way a replacement to Node. But, it's a fun thing nonetheless. I'll totally play around with it.
  • 1
    @IHateForALiving people want to do this because their intellectual capacity is at the level of a 5-year-old.

    so they're unable to understand that javascript is terrible in any and every way.
  • 1
    @kiki I won't use it but share your enthusiasm. It's awesome
  • 2
    Could be cool for a featherweight vendor-independent lambda alternative
  • 0
    Although the startup times are probably dysmal, what with the inability to retain language constructs across requests. Any and all metaprogramming has to be re-processed every request.
  • 0
  • 1
    @lorentz come on man, it's not a node replacement. First, startup times have to be benchmarked. Second, the only thing I need from those JS scripts is to get a request data and assemble an HTML page. You don't need much for that.
  • 0
    The real pain starts when you want to configure NGINX for better security...

    You will miss a lot. You will learn things you thought would never learn before.
    You will descend into a vast dungeon bigger than molten core.

    And then there is the enterprise version...
Add Comment