Objective
It is said that Nodejs is suitable for making restful_api, because it has the ability of asynchronous processing, can make more requests, this is recognized by everyone. Not long ago I used nodejs+express+postgresql to build a restful intermediate processing platform, feeling that the API of Express is messy when the code volume is much. Then I developed a module that would better organize the express code, drawing on some ideas from Java. Want to achieve a balance, a more elegant way to organize your code.
Express
The coding process for an express project is probably a few steps,
1. Initialize the app (Express ())
2. Introduce routes to introduce routing, then route distribution router
3. Define Router
4. Write responses such as Methods,get, post, etc.
5 Front-End requests
Take my own project look, this is a router:
var express = Require ("Express"); var router = Express. Router (); var config = require ("Config-lite"); var check = require (".. /lib/check "); var select = Require (".. /db/select "), Router.get ("/", check, (req,res,next) ={ res.render (" index ", { pageName:"Jiankong" });
Router.post ("/", check, (req,res,next) ={ res.send ({ msg: "Jiankong" })});
= Router;
What's wrong with that? --Contemporary code size increase, all kinds of app.get. Post heap in a folder, or split these into a large number of files. This is obviously detrimental to the maintenance of the code.
My solution
I need to solve the problem of writing a restful or Web application when the code is not easy to understand. So I wrote a module:Ting.js.
The features I need:
1. Ability to generate documents based on code
2. Clearer code logic
3. Preferably router can be configured directly
After more than half a month of development. Now my code has changed all of my projects. I think its code logic is clear enough. For example, the definition of router:
Module.exports = [ { brie:"Home", desc:"This is the overall description of the homepage", Path:"/", Class:home, rules:{ // object get:[ { Brie:" This is the homepage of the Get access method ", desc:" returned Hello World ", Path:"/", controller:[" Gethome "] } ] } } ]
This code configures a path is/router where it has a get method, and the handler function is the Gethome method of class home.
Interested? Then take a look at the following tutorial, run a demo try
Ting.js
Ting.js for 1.1.12 Express-based server engineered Components Ting.js Brief introduction Ting.js is an express-based server engineering component that is primarily designed to simplify the more complex router operations of Express, making Nodejs server-side APIs more friendly. Add-ons can generate routing documents in real time, making API requests at a glance, concise code while maintaining express scalable performance. Before you begin
Before everything starts, you need to have the knowledge of Express, and in addition, you need to generate a copy of Package.json.
When you're ready, let's get started!
Step 1: Download Express
You need to download Express first:
Project directory Open command line run NPM install Express--save
Step 2: Download ting.js
You need to download Express first:
Project directory Open command line run NPM install ting.js--save
Step 3: Create a Index.js
Entry file
Const PATH = require ("path"); //Introducing ExpressConst EXPRESS = require ("Express"); //Express_app varApp =Express (); varTing = require ("Ting.js"); //Define Routes varRoutes = require ("./routes"); //introduction of _package var_package = require ("./package.json"); //Doc Path_package.doc.path =Path.join (__dirname,_package.doc.path); //Initialize varTING_FN =function(init) {init (routes); } //Generating RoutesTing (app,ting_fn,_package); //MonitorApp.listen (8090, () ={Console.log ("Ting_server started 8090"); });
Step 4: Create a Routes.js
Configuring Routes Rules
//Processing Class varHome = require ("./home.class"); //Defining RulesModule.exports =[{brie:Home, desc:"This is the overall description of the homepage", Path:"/", Class:home, rules:{//Objectget:[{brie:"This is the Get access method for the homepage", desc:"Back to Hello World.", Path:"/", Examination:true,//Open Code Viewcontroller:["Gethome"] } ] } } ]
Step 5: Create a Home.class.js
Instead of complex router, use class files as request portals
class home{ main () { } gethome (req,res,next) { res.send ("Hello World"); } } = Home;
Step 6: Run the test
Project directory Open command line run
Node Index
Visit localhost:8090
You can see the class home called the Gethome, and the smooth return to Hello World,
It can also generate documents that generate HTML documents at each launch based on the version in the package, the configuration method in the API, or in the Serverdemo on my Gethub, which is attached to the resource column.
Summarize
I think Ting.js can also work on Doc, such as tagging the code, labeling classes, and adding comments using code. Another way to configure the file, through the intermediary services, to achieve the effect of multi-person development, these are what I want to do later. But now I still hope to have a friend to submit a few issues, perfect the current code
Https://github.com/294678380/Ting.js_server_demo/issues
Resources
Gethub:https://github.com/294678380/ting.js
Api:http://www.tingjs.top/api
A demo of the Github:https://github.com/294678380/ting.js_server_demo
"Nodejs's My Open source module" uses Express to build a Web server, the code is the implementation of the document.