2
0

A radical approach to web development with node.js


 invite response                
2019 Jan 19, 12:01pm   647 views  2 comments

by Patrick   ➕follow (55)   💰tip   ignore  

I've been working on a genealogy site for my family. It's written in node.js in a model loosely based on React.

The React part is the idea that there are only state and reactions (thus React!) to changes in state. There isn't any "narrative flow to the story" the way people often write programs, ie, this happens, then this, then this...

The state is the family tree. "Actions" are applied to the state. And action consists of a url with all the headers, and potentially some POST or GET data. Upon receiving an action, the server updates the state and returns a newly rendered web page based on the new state.

All input and output (network or file system) happens only in the small main function. All other functions are "pure", meaning that they depend on nothing but their formal parameters, and they do not modify those parameters or interact with the network or filesystem. They simply return a value. This makes them extremely easy to understand and reason about as discrete units.

There is no package.json at all, for better security and simplicity. I had some security training at work in which the presenter said that there is a trend away from dependency on npm or other archives of modules. Instead, you just write what you need, or just read, understand, then manually copy in the code you want from one of those packages. No security concerns, no package skew, no dependency on leftpad, etc.

To be even more brutally simple, I made the site a single file of source code. No more searching around in various files. Since almost all of the functions are pure, they cannot interact with each other except through explicit calls. No side effects, no globals. So why not include them all in the same file?

One more thing: I just don't like camelCase, the usual way of writing variables in javascript. Instead everything is snake_case for improved readability.

Comments 1 - 2 of 2        Search these comments

1   FortWayneAsNancyPelosiHaircut   2019 Jan 19, 12:30pm  

Patrick this sounds cool, how do you avoid dependencies sounds pretty cool. But I don’t understand how that works. Do you just keep sources in a directory and reference?

I do have some basic knowledge of node.
2   Patrick   2019 Jan 19, 12:34pm  

All source is in just one file. So to find anything, you just use your editor's way of searching. In vim, you can just * over a character to find the next occurrence, for example.

The only other thing in the directory is the state file (JSON in my case). You could use a database like mysql instead of that. Then the database would be your "state file"

Please register to comment:

api   best comments   contact   latest images   memes   one year ago   random   suggestions