Enterprise Application Final Project : Node

In the last part of our Enterprise Application Final Project series, we discussed mostly on the Angular side. Today we will be handling the Node side using Node, Express 4 and its Router, and MYSQL for the database.

========================================================

Here is what we’ll be doing:

  • Handle CRUD for a recruitment (we’re going to use local database)
  • Use the proper HTTP verbs to make it RESTful (GET, POST PUT and DELETE)
  • Return JSON data

Before we continue, make sure that you have Node installed!

========================================================

Structure

Here is our file structure.

config
  - dbServer.json
models
  - m_recruitment.js
routes
  - recruitment.js
main.js
package.json

========================================================

Defining Node Packages

We will define the packages we need in package.json

{
  "name": "EA_Project",
  "version": "1.0.0",
  "keywords": ["util", "functional", "server", "client", "browser"],
  "contributors": [],
  "dependencies": {
      "body-parser": "~1.13.2",
      "express": "~4.13.1",
      "jsonwebtoken": "^6.2.0",
      "mysql": "^2.10.2",
      "redis": "^2.6.0-2",
      "sequelize": "^3.3.2"
      }
}

body-parser will let us pull POST content from our HTTP request.

express is the Node framework

jsonwebtoken is a compact token format intended for space constrained environments such as HTTP Authorization headers and URI query parameters.

mysql is the Relational Database Management System (RDBMS)

redis is a data structure server, that can contain strings, lists, sets, hashes and other data structures.

sequelize is the ORM for Node

========================================================

Installing Node Package

Now, go into the command line in the root of your application and type this

npm install

npm will now pull in all the packages defined into a node-module folder in our project.

What is npm?

npm is Node’s package manager for the Javascript runtime environment Node.js. It will bring us all defined packages into our project.

========================================================

Setting Up Our Server

We will set our server. Insert the following code to your main.js

//SETUP 
//=====================================================================

//call the packages we need
var express = require('express');  // call express
var app = express(); //define our app using express
var bodyParser = require('body-parser');
var http = require('http');
var fs = require("fs");
var path = require("path");
var models = require("./models");

var server;
var port = process.env.PORT || 8082; // set the port

var recruitment = require('./routes/recruitment');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function (req, res, next) {
 console.log(req.originalUrl);
 console.log(req.body);
 console.log(req.params);
 res.setHeader('Access-Control-Allow-Origin', '*');
 res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
 res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Access-Token');
 
 next();
});

app.options('*', function(req, res) {
 res.sendStatus(200);
});

//REGISTER OUR ROUTES
//=====================================================================
app.use('/recruitment', recruitment);

Setup In the setup part, we pull in all the packages that we get when we did npm install. We can change the port, as long as it is not used by other application.

========================================================

Database

Creating Database and Syncing

Now you have to create your local database. For this project, I set the name as ea_project. We will defined our database in dbServer.json

{
 "username": "root",
 "password": "",
 "database": "ea_project",
 "config": {
     "host": "127.0.0.1",
     "dialect": "mysql"
     }
}

Let’s continue to set model to our database. Add this code to your main.js

//SYNC THIS MODEL TO THE DATABASE //===================================================================== models.sequelize.sync({force: true}).then(function () {
 server = http.createServer(app); server.listen(port);
 console.log('Server started '); 
});

Sync This Model to The Database We sync our model to our database. The console will simply print out.

========================================================

Modeling

For this project, we will create for recruitment. Therefore the only model we need to create is recruitment. Add the following code to your m_recruitment.js

"use strict"

module.exports = function(sequelize, DataTypes) {
   var Recruitment = sequelize.define("Recruitment", {
   name: {
     type: DataTypes.STRING,
     allowNull: false
   },
   address: {
     type: DataTypes.STRING,
     allowNull: false
   },
   contact: {
     type: DataTypes.STRING,
     allowNull: false,
   },
   email: {
     type: DataTypes.STRING,
     allowNull: false
   },
   refferer: {
     type: DataTypes.STRING,
     allowNull: true
   },
   position: {
     type: DataTypes.STRING,
     allowNull: false
   },
   pay: {
     type: DataTypes.STRING,
     allowNull: false
   },
   company: {
     type: DataTypes.STRING
   },
   caddress: {
     type: DataTypes.STRING
   },
   ccontact: {
     type: DataTypes.STRING
   },
   cjob: {
     type: DataTypes.STRING
   },
   school: {
     type: DataTypes.STRING,
     allowNull: false
   },
   saddress: {
     type: DataTypes.STRING,
     allowNull: false
   },
   scontact: {
     type: DataTypes.STRING,
     allowNull: false
   },
   sadditional : {
     type: DataTypes.STRING
   },
   rname : {
     type: DataTypes.STRING 
   },
   roccupation : {
     type: DataTypes.STRING
   },
   rcontact : {
     type: DataTypes.STRING
   }
 }, {
     id: false,
     updatedAt: false,
     createdAt: false
  });
  return Recruitment;
};

Those code will create a table in our database with all field and conditions as listed.

========================================================

Routing

Here is an overview of the routes we will require, what they do and the HTTP Verb used to access it

Route HTTP Verb Description
/recruitment GET Get all recruitments data
/recruitment POST Add a recruitment

These following code simply add and get data of recruitment. Insert these code into recruitment.js

var express = require('express');
var router = express.Router();
var models = require('../models');

var Recruitment = models.Recruitment;

//Get all recruitments
router.get('/', function (req, res) {
   Recruitment.findAll()
   .then(function(result) {
   res.status(200).send(result);
   });
});

//Add a recruitment
router.post('/', function (req, res) {
   Recruitment.create({
     name: req.body.name,
     address: req.body.address,
     contact: req.body.contact,
     email: req.body.email,
     refferer: req.body.refferer,
     position: req.body.position,
     pay: req.body.pay,
     company: req.body.company,
     caddress: req.body.caddress,
     ccontact: req.body.ccontact,
     cjob: req.body.cjob,
     school: req.body.school,
     saddress : req.body.saddress,
     scontact : req.body.scontact,
     sadditional : req.body.sadditional,
     rname : req.body.rname,
     roccupation : req.body.roccupation,
     rcontact : req.body.rcontact 
   }).then(function(result) {
     res.status(200).send({
       status: 'Data inputed successfully!'
     });
   });
  });

module.exports = router;

========================================================

Now you have finished the back end part.

If you have done with the Angular connected to database part, you only need to run your Xampp, your Back End part (Node) and run the Front End part (Angular)

Go to recruitment page in the menu, then fill the field. For example : input recruitment data

After that, login as admin and see the Recuitment part.

If your inputed data is available there, then you have successfully finished this project

List of recuitment

 

*********************************************************

Chelsy                           1701319994                                                                   Evelyn Kharisma          1701320825                                                                   Fansi Lantana               1701341741                                                                 Janis Giovani Tan         1701320560

Leave a Reply

Your email address will not be published. Required fields are marked *