Home » Nodejs » Raw Node.js without any web framework [closed]

Raw Node.js without any web framework [closed]

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m trying to learn node, I saw lot of material (as suggested in that famous SO question), the problem is that all the books I saw, or tutorials either use a web framework like express or simply limit themselves to explain what node is and go no deeper than explaining how to create a very basic http server which listens for requests on some port.
So I’m really wondering is there anybody who uses node w/o a web framework? If so they must have learned it somewhere, so could you please suggest where can I learn it?
I know that this is very low level, but I don’t mind, I have already a familiarity on how to create servers in C.
I’d really like to understand how we really serve static content with node (customly organized in folders) and how we actually introduce logic into our html (I looked all this up but found only results on how to do it using Express, where logic is introduced by something like <% //code %>, but can this be done in pure node?).

The question similar to mine which has been already asked here does not really have an answer. The best answer just suggests to read the official nodejs documentation which is like saying:
there you have a dictionary with millions of words in Latin, good luck in learning it.

So can you suggest some well documented, robust book/ tutorial where is shown how to create a real world website using raw node?

If not, I guess I’ll have to stick to php + Apache and try to optimize those to scale.

Answers:

Node.js by itself is a Javascript execution engine (based on V8) that runs on many different platforms and comes with a standard library. It’s somewhat analogous to any other interpreted language with its standard library (such as Python or PHP). It would not be accurate to describe plain node.js as a “web platform” by itself. It has the core tools where one can make a web platform out of it, but it can also be used for all sorts of other types of uses that have nothing to do with being a web platform. For example, I’ve built some command line build tools out of it for doing various forms of text processing (a use that does no networking of any kind).

So I guess “raw” node just means solving whatever problem you want to solve without building on top of 3rd party libraries (beyond the standard library that node.js comes with). Personally, I’m not sure why you really want to do that. One huge advantage of node.js development is the whole NPM eco-system where there are thousands of pre-built, free and open source modules that solve thousands of problems. Some are a few functions, but still useful and others are whole APIs that solve rich sets of problems. The beauty of NPM and this eco-system is that with one simple command you can add any one of these modules to your project and with a couple lines of code, you can be using it in your project. I’d consider it silly to avoid this advantage.

So learning raw node.js means learning Javascript, learning the tools often used for node.js development (debugger, NPM, console, etc…) and learning the standard library that comes with node.js. Few people ever want to sit down and actually learn every function in the standard library. Usually what people do is take a good long look at all the modules available in the standard library, page through each one to get a sense of what kinds of things they have in them and then find yourself something you want to build and start building it. As you are forced to find things and figure out how they work from the documentation or from Google searches or from studying other node.js code you find, you will learn how portions of the standard library works and what it does. If you are doing I/O (file, network, etc…), you will quickly encounter lots of asynchronous APIs in the standard library and you will generally want or need to become proficient at handling async operations (which is really just learning async Javascript), but will likely be important in a node.js project.

If you really want to “study” the standard library by itself, then both Amazon and Google have long lists of resources you can page through to see which ones seem to approach things the way you want to. Asking for us to find such a resource for you is considered “off topic” here on StackOverflow so I will leave you to go consult those lists and decide what looks interesting. I myself knew client-side Javascript and picked up node.js by just reading some web resources and then working on my own projects. Eventually, I built a node.js system that runs on a Raspberry Pi and sits in my attic monitoring temperature probes, switching attic fans based on temperature differences and offering a web interface for controlling, configuring and reporting on everything that is going on. It’s half a web app and half a stand-alone temperature controller.

Unfortunately, the documentation for the node.js standard library is not richly descriptive (I’m being kind there). It’s technically accurate, but often doesn’t answer many of the common questions anyone wanting to use an API would quickly have. It appears to assume that you already know a lot of the Unix C standard library as it has many similar functions (particularly for file access). Plus, one of the documentation drawbacks of an object hierarchy (where things inherit from other things) is that it can be hard to put together in one place everything that a given object does. Instead you have to mentally collect and understand what the base objects do and then try to figure out how that fits in to what the root object is doing. This isn’t a challenge that only node.js faces, lots of object oriented systems have this documentation challenge (it used to drive me nuts with YUI).

What I quickly found is that I really needed to be able to find the relevant library source code for any standard library function so I could look at how the function was actually written in order to answer my questions. This is such a huge advantage of both open source and how searchable GitHub is (all the standard library source is on GitHub).

So, page through all the modules in the node.js standard library to familiarize yourself with what is there and where you’d find it. Then find yourself an app you want to build and build it. If you’re building a web app, I can think of no reason why you would ever want to do that without using a framework someone else has already built for you (I use Express). There’s just no reason to reinvent everything yourself. If you want to make sure you understand the HTTP module before using Express, then build yourself a simple web server using only the HTTP module that serves two static files and use the HTTP module going the other way to request a couple web pages from other servers. Then, start using Express to build your own web app.

As for some of your more specific questions:

I’d really like to understand how we really serve static content with
node (customly organized in folders)

Some of the answers to this question show you a basic node-only web server that serves up static content from a file system. More examples and explanation here.

how we actually introduce logic into our html (I looked all this up
but found only results on how to do it using Express, where logic is
introduced by something like <% //code %>, but can this be done in
pure node?).

Introducing “logic” into your HTML from scratch means that you first build a system for serving static web pages and then you add to that a system for parsing through the web pages (on the server) to find directives in them that mean your server should do something to modify or add to the HTML before sending it to the client. There are many different ways of doing this which is why there are probably 50 different systems out there already for doing this. A search for node.js template systems will find you a list. Again, I have no idea why you would want to build one of these from scratch. It is a bit of a research project to figure out which of the zillions out there matches your desires the best (I’m using Handlebars, a derivative of Mustache myself), but that should be way, way less work than building your own system from scratch. And, even if you want some grand capabilities that can’t be easily accomplished with a stock system, you may as well start with a stock system and extend it.

Questions:
Answers:

What you’re asking for seems to be two different things, you want to do raw Node, but at the same time know how people use it in the real world. Pretty much everyone uses at the very least, Express (or Koa if you’re on the bleeding edge) as their web framework. They provide the bare bones to create a robust web server. Then, for your actual front end, you’d use either AngularJS or ReactJS, you wouldn’t do any rendering server side template rendering (e.g. no <% %> code blocks). If you choose to go the React route, you’ll need additional libraries like BaconJS to assist with the glue, as React is just the view layer, whereas Angular is an all encompassing MVC framework.

One of the best ways to learn, is to look at the MEAN stack yeoman generator https://github.com/DaftMonk/generator-angular-fullstack . Use the generator to make an app, and read its source to see how it is structured, and just start hacking away at it to get to do what you want.