Node.js Framework for MyWeb Development: An Introduction
Welcome to the Node.js Forum
In this special issue, we will delve into the development of MyWeb using the Node.js framework. This framework employs the MVC (Model-View-Controller) model for web development, utilizing the Express framework as a web developer’s support tool, along with MySQL as the database development source. We will explore how to use this framework to develop a simple web application.
Project Folder Structure
The project folder is primarily based on the traditional MVC design pattern, with a well-structured framework. The main folders include:
- application: stores common files for the application layer
- common: stores common files for the application
- controller: stores the logic layer for the application
- core: stores the base class files for the application
- model: stores the data processing layer for the application
- conf: stores configuration files for the application
- log: stores log files for the application
- public: stores public files for the application
- view: stores view files for the application
Directory Structure
The directory structure is as follows:
- application
- common
- controller
- core
- model
- conf
- log
- public
- view
Global Variables
In the index.js
file, we define global variables to store the folder paths and module names. This approach avoids the need to introduce long file paths in the code and makes it easier to maintain.
// Define global variables
global.BASE_DIR = __dirname;
global.APP = global.BASE_DIR + "/application/";
global.CON = global.APP + "/controller/";
global.CORE = global.APP + "/core/";
global.MODEL = global.APP + "/model/";
global.CONF = global.BASE_DIR + "/conf/";
global.log = global.BASE_DIR + "/log/";
global.PUBLIC = global.BASE_DIR + "/public/";
global.VIEW = global.BASE_DIR + "/view/";
// Import modules
global.express = require("express");
global.sio = require("socket.io");
global.fs = require("fs");
global.path = require("path");
global.url = require("url");
global.parseCookie = require("connect").utils.parseCookie;
global.MemoryStore = require("./node_modules/connect/lib/middleware/session/memory");
global.Session = require("./node_modules/connect/lib/middleware/session/session");
global.sys = require("util");
Routing Processing Logic
The getActionInfo
function is the main entry point for the routing processing logic. It exports the getActionInfo
function and defines private methods for processing URL requests.
// Export the getActionInfo function
exports.getActionInfo = function () {
systemConfig();
app.get("/:key", function (req, res) {
callUrlRequest(req, res);
});
app.post("/:key", function (req, res) {
callUrlRequest(req, res);
});
listenPort();
};
// Private methods
function callUrlRequest(req, res) {
var routerMsg = getUrlConf();
var key = req.params.key;
var session = checkSession(req, key);
if (key == "favicon.ico") {
return;
}
if (session == 0) {
res.redirect("/index");
return;
}
console.log("[key:" + key + "]" + "[class:" + routerMsg[key].cla + "]" + "[controller:" + routerMsg[key].fun + "]");
require(CON + routerMsg[key].con);
var controllerObj = eval("new" + routerMsg[key].cla);
controllerObj.init(req, res);
controllerObj[routerMsg[key].fun].call();
}
System Configuration
The systemConfig
function configures the Express framework with the corresponding data frames and static configuration data.
// System configuration
function systemConfig() {
// Configure Express framework
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: "secret", store: new MemoryStore() }));
app.use(express.static(__dirname + "/public"));
app.use(app.router);
});
}
Database Configuration
The getUrlConf
function retrieves the routing profile information from the configuration file.
// Get routing profile information
function getUrlConf() {
var routerConf = require(__dirname + "/conf/routerConf");
return routerConf;
}
Controller, Class, and Function
The callUrlRequest
function requires the corresponding controller, creates a new object using eval
, and calls the specified function.
// Require corresponding controller
require(CON + routerMsg[key].con);
var controllerObj = eval("new" + routerMsg[key].cla);
controllerObj.init(req, res);
controllerObj[routerMsg[key].fun].call();
Listen Port
The listenPort
function starts the server listening event.
// Start server listening event
function listenPort() {
app.listen(3000, function () {
console.log("Server started on port 3000");
});
}
Data Layer
The data layer is implemented using the base_model.js
file, which contains methods for database operations such as add, update, deleteItem, query, and select.
// Base class for data layer
function BaseModel() {
// Database operations
this.add = function () {};
this.update = function () {};
this.deleteItem = function () {};
this.query = function () {};
this.select = function () {};
}
Logical Layer
The logical layer is implemented using the base_controller.js
file, which contains methods for the application logic.
// Base class for logical layer
function BaseController() {
// Application logic
this.init = function () {};
this.displayHtml = function () {};
this.displayJade = function () {};
}
Code Specification
The code specification is implemented using the following rules:
- Variable naming: private variables use underscore notation, global variables use uppercase notation, and simple variables use camel case notation.
- Method naming: all methods use camel case notation.
- Class naming: all classes use capitalized camel case notation.
- File naming: all files use underscore notation for class division.
This concludes the introduction to the Node.js framework for MyWeb development. The framework uses the MVC model for web development, with the Express framework as a web developer’s support tool, and MySQL as the database development source. The framework provides a basic structure for developing web applications, including routing processing logic, data layer, logical layer, and code specification.