router-plan
v1.1.3
Published
Another way to achieve routing, management projects more convenient.
Downloads
2
Readme
router-plan
Another way to achieve routing, management projects more convenient.
advantage
- Clause clearer, a type of routing is a file.
All requests are go through a common method, easy to handle.
- Interface only one, different requests only incoming parameters are different.
use
npm install router-plan
The file directory is as follows:
- router-list --- Storage routing folder
- A --- routing folder
- a.js --- Specific routing documents
- index.js --- Expose the current path of all the routing files
- index.js --- Expose the current route all the routing files
- server.js
server.js file format is as follows, after the success of the creation of the server, you only need to call routerPlan () this method and the incoming corresponding parameters, the other automatically help you achieve.
const http = require('http');
const routerPlan = require('router-plan');
http.createServer((req,res) => {
routerPlan(req, res, {
routerList:require('./router-list'), //Routing file address
crossDomain:true //Whether to allow cross-domain
});
}).listen(80,() => {
console.log('server start ok!');
});
router-list folder index.js below, mainly used to expose the file inside the router-list folder, used to generate json object.
const fileList = ['a','b','c'];//router-list below all the folder name
fileList.forEach((fileName) => {
module.exports[fileName] = require(`./${fileName}`);
});
A folder index.js below, mainly used to expose all the files inside the A folder, used to generate json objects
const fileList = ['a','b','c'];//A below all the folder name
fileList.forEach((fileName) => {
module.exports[fileName] = require(`./${fileName}`);
});
'a.js' file format is as follows: Route file wording, the methods are exposed to the corresponding upper object, through the exports.functionName (method name) to write the interface.
exports.a1 = function (query,req,res) {
res.end('this is fileName:c,fn:a1');
};
exports.a2 = function (query,req,res) {
res.end('this is fileName:c,fn:a2');
};
Client request parameters are as follows:
eg: A request a folder inside the a1 method
$.ajax({
url: 'http://localhost',
type:'post',
data: {
folderName: "A", //Router-list folder below the folder name
fileName: "a", //A folder below the file name
fn: "a1" //A file name inside the method
},
success: function (data) {
console.log(JSON.parse(data));
}
})