Building my own NodeJS MVC Framework — Part 1

  • Keep it simple. No giant integrations, huge helpers or super complex tasks.
  • Do MVP (minimum viable product). Less is more.
  • Be opinionated
  • Easy to use (really easy)
  • Include all the fun stuff: template parser, ORM (Object Relation Mapping) and etc.
my_framework/
- config/
- controllers/
- models/
- public/
- images/
- javascripts/
- stylesheets/
- src/
- middlewares/
- tests/
- views/
- layouts/
  • http to handle the browser request/response
  • fs to load internal files.
  • ejs to handle our template parsing
  • node-static to serve static files (css, js, 500.html)
  • pg to handle the database connection. We'll be using PostgreSQL as our database.
  • A general logger (debug, info, error)
  • Post parsing (if we submit a form, we need to be able to handle the body data chunks)
  • Cookie parsing (persist data)
  • Flash handling (we want to exchange messages that should live for just one day)
  • Data handling. Providing an easy to use ORM and connecting to the database. The ORM should be able to query, insert, update and delete data.
  • Routing!
  • Autoload (for the minimal magical piece, we’ll be loading all the controllers to be magically found). This will keep the Router short and simple.
  • Response. It should be smart enough to handle 200 (success), 301 (redirection), 404 (not found), 500 (error) and custom responses.
  • Static file serving. node-static will be responsible for this, but we have to tell it how to act.
  • And, finally the most fun part in my studies: middlewares. Post parsing, cookie parsing and flash handling are actually middlewares. And we can always build new ones to happen before and after the controllers, adding a really neat execution structure.
  • this._tableName (posts, in our case)
  • this._beforeSaveHooks ([])
  • this._afterSaveHooks ([])
  • this._validationHooks ([])
  • this._attributes ({})
  • this._changedAttributes ([]) // to only update what we did actually changed
  • this.errors ([]) // or general error controller
  • this._tableName = this.constructor.name.toLowerCase() + “s”; // will transform Post to posts.
  • this._attributes[‘id’] = {type: “integer”, value: null }; // our id
  • this._attributes[‘createdAt’] = {type: “datetime”, value: null}; // createdAt timestamp handle
  • this._attributes[‘updatedAt’] = {type: “date time”, value: null}; // updatedAt timestamp handle

--

--

--

Software Engineer. www.leonardopereira.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JavaScript ELI5: Null vs Undefined

A laptop sitting on a desk, with code syntax displayed on an otherwise dark screen

How to install react.js in linux

Content Management System with Grape JS and Keystone JS

LWC Way to pass the data between component, Parent to Child, and Child to Parent component

Useful Anaconda Commands You Should Know About

Conditional Rendering

How to run and build webpack

Takeaways from React Native EU 2019

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Leonardo Pereira

Leonardo Pereira

Software Engineer. www.leonardopereira.com

More from Medium

Fix React start command Connection Refused on Docker-Compose

WebSocket development with Typescript from scratch

Getting Started with NodeJS

Upload files with any Cloud Storage Service with just 10 lines of code