This example will show you how to create a Node.js Restful API for the backend, and it will be called by the frontend – AngularJS. The Node.js Restful API will response with JSON output to AngularJS frontend. You will find that it is so simple to do so. You will find that the folder structure
Like other famous development languages, such as .NET and Java, node.js does have development framework like MVC. This example will show you how to buildup a small MVC web application using express framework. Please note that this example is based on express version 4.13.4 . Let’s create the folders: example under {your_node.js_installed_path}\nodejs\ demo2 under {your_node.js_installed_path}\nodejs\examples\
This example will show you how to connect to MySQL database and response output to webpage. Please download the schema.sql here, and run it with MySQL client. /* create database */ CREATE DATABASE itblogs DEFAULT CHARACTER SET utf8; USE itblogs; CREATE TABLE COMPANY( id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, background
Throughout our examples, some required npm packages need to be installed, some of them can be installed by npm – build-in package management tool, and the remaining are bundled with Node.js . Please find the below packages and their details: Package Express NPM Install npm install express Functions Provide the coding framework Usage Create the
Before working on the examples, let’s setup the environment first: Install MySQL (using 5.7 for our example), you can download from http://dev.mysql.com/downloads/mysql/ Download and install Node.js version 4.X from https://nodejs.org/en/ Run “cmd”, and using it to install necessary modules Install Express framework by “npm install express” Install ejs template engine by “npm install ejs” Install logger Morgan by “npm install morgan”
Why use Node.js? You may hear people saying that node.js is not good, poor performance, and not for enterprise application. Before answering this question, we better to have a look to the below pros and cons. Then, we can conclude what kind of application is good to build with node.js, and what is not.
Now, everything is ready, let’s try to run the application and see what is happening… Right-click the project, select “Run As” => “Run on server” With URL : http://localhost:8080/SpringMVCExample/ , You can see … For this web UI, it is using AngularJS, Angular Material, bootstrap and ng-Table to buildup, please reference to the
What is Spring MVC Controller? It helps to: process user requests based on the requested URL call Service Manager => DAO for some database actions, such as insert/update/delete/query output the query result to valid view (JSP) What if my application need to be multiple users? By default, scope of Spring controller is Singaton, only
All DAO classes will extend the GenericDao.java, which includes some common database actions, such as saveOrUpdate() and findByID(). Besides, it is also using to auto-wired the sessionFactory: package com.itblogs.dao; import java.util.List; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Order; import org.hibernate.Criteria; import org.springframework.beans.factory.annotation.Autowired; /** * @author Kelvin Hui * @see it-blogs.com *
We will create five model classes mapping to our database tables, please download here: Java Class Files Table Name Address.java ADDRESS Customer.java CUSTOMER Orders.java ORDERS Product.java PRODUCT Shop.java SHOP Nil We don’t need to create a model class for this table. But, we will use @ManyToMany annotation in Orders.java to state this join table