As we've seen before, our framework is still missing some important pieces. As it has no idea how to handle form data or even cookies, which are substantial information for a working framework to deal with. We need to be able to exchange data besides route paths and what is in our database.
To include this feature, we need to use the power of Middlewares. But we don't even have the code for it. So, let's start designing how our PostsController#create would act to have some ground to design our tool.
controllers/PostsController.js and include a new method in it:
The framework and the server. Real world coding!
Before starting this, let's add all packages we're going to need. I'll explain all about them later as we use.
So, run in your root directory the following command:
npm install esm ejs node-cookie node-static path-to-regexp string_decoder --save
Add 3 new files in the
Router.js . Also add a new folder
With this done, let's use the power of
esm to translate our import/export correctly, since some of the packages only responds to
require() and I actually want to use fresh new ES6 code.
In order to start building our Model.Base class to be extended in each model, we need to set up our “helpers”. They should be 3.
Model.Attributes (our constant data types, they we'll be used only for reference by now but we can apply this in the future as strong types.),
Model.Validators a set of small functions to validate attributes easily. And finally (and most important)
Model.Client our PG.Pool client using
pg package to handle the database connections and response. We'll be using all the power of query() to also sanitize our data :) security first! …
Hello, all. Recently I’ve re-started my NodeJS studies, as I’m joining a new company and I’ll be working daily with NodeJS instead of Ruby on Rails (my main language of expertise). As a way to improve my knowledge, I’ve always enjoyed building and rebuilding things. I’m a hard believer that learning comes from creativity, so, let’s create something!
Quick notice 1: This will be a long tutorial! Quick notice 2: This is not intended in any way to replace a well stablished framework such as ExpressJS or SailsJS, it's designed for learning purposes only, use something stable for your production…