The Express-Starter in a Structured Way
In this article, we cover how to create an application that will fetch movie names from an unstructured, PostgreSQL database.
Join the DZone community and get the full member experience.
Join For FreeWhy Express Starter?
Normally boilerplates like MEAN.io or MEAN.js or any other, use MongoDB or some other unstructured database.
Using unstructured databases provides the developer with the ability to manipulate the database in JSON format. Well, the mapping part is a bit tricky when it comes to unstructured databases, and the manipulation of structural databases can be made easier by querying the necessary info (for example – select * from tableName). In such cases, great results can be achieved when we use Node.js with the structured database, PostgreSQL.
Express Starter provides you with a view using Angular and Express.js for routing, and Node.js on the back-end, along with that the storage partner PostgreSQL.
Codebase Example (With Relation to Mapping)
The following example shows fetching a list of movie names right from a database to your view.
1.) Inside Model (Schema/Sequelize)
module.export = function(db, DataType) {
var movieList = db.define(‘movies’, {
id: { ‘Provide the dataType and other attrbiutes over here’},
movieName: {‘Provide the dataType and other attrbiutes over here’}
});
}
2.) Inside Repository
var repo = {}; //Empty object
var db = require(‘Path to sequilize file which we made’); // call the db object which indirectly calls the sequilize schema object called(‘movieList’).
repo.movieList = function() {
var returnMovie = db.movieList.findAll();
return returnMovie;
}
3.) Inside Controller
var db = require(‘path to db defined in sequilize’);
var movieRepo = require(‘Path to Event Repo’);
exports.movieListController = function(request, response) {
movieRepo.movieList().then(function( movieName) {
res.render(‘/moviesList’, { movieDisplay: movieName});
});
}
4.) Inside app.js (Mapping Controller With View)
var express = require(‘express’);
var app = express();
var movieController = require(‘Path to the controller which we made previously’);
app.post(‘/event’, movieController.movieListController) ; //Mapping the views with the controller by sending an api(using POST request).
5.) Mapping the Movie Names With the movieDisplay.ejs File (Displaying Movie Names).
<form method=”post” action=”/moviesList”>
<div><%= movieDisplay.movieName %>
</form>
Folder Structure (From the Route Directory)
Config – Configuration for a passport or any other secret key that needs to be provided.
Controllers – Logical arena where the mapping between the models occurs (by exporting the repositories) and where we call it in view.
Migrations – Creating tables using the schema’s provided in the model.
Public – Contains a list of all Stylesheets, Images, and JavaScript for the view.
Repositories – Contains individual files for each module consisting of querying in order to fetch the appropriate data.
Seeders – Made up of 'by default values' which need to be set in the individual records. For example, if we need to assign a username and password (master username and password), this allows us to seed the created username/password record so that it’s fetched right away.
Services – Consists of the services (Generic functions) which we need to use in our various controllers.
Views – Consist of .ejs files with the main file named layout.ejs, wherein we import the other sub .ejs files.
app.js – Route for the controllers to be sent to the views (the entire mapping is done here).
package.json – Necessary modules can be installed using npm.
About the Existing Project Made in Express Starter
An internal CRM for all employees wherein employees can apply, leave, see events, and more. Having 4 roles involved (Including – Admin, HR, Manager, Employee) the project is under development with new features coming up.
For any query with relation to express starter, you guys are most welcome to view my GitHub repo.
Happy expressing!
Published at DZone with permission of Sibu Stephen. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments