Status quo: Redundancy
In web development, do we often use different programming languages to implement the same functions?
For a file upload function, you must restrict the format of the file to be uploaded. We usually use the extension name for restrictions.
Front end
For the user experience, the user will judge the selected files on the page, so that the user can upload the files only after they are valid.Copy codeThe Code is as follows: function is_filetype (filename, types ){
Types = types. split (',');
Var pattern = '\.(';
For (var I = 0; I <types. length; I ++ ){
If (0! = I ){
Pattern + = '| ';
}
Pattern + = types [I]. trim ();
}
Pattern + = ') $ ';
Return new RegExp (pattern, 'I'). test (filename );
};
// N lines of code are omitted here
If (! Is_filetype ($ ('# uploadfile'). val (), 'doc, pdf, txt, wps, odf, md, png, gif, jpg ')){
Can_submit = false; // upload is not allowed
$ ('# Uploadfile'). val ('');
Alert ('upload only allowed: '+ constant. RESUME_FILETYPES );
}
// N lines of code are omitted here
Backend
Due to fear of malicious uploads, it is unavoidable to re-judge the files uploaded by users. So I used python to write a logic for determining the file suffix.Copy codeThe Code is as follows: import re
Def is_filetype (filename, types ):
Types = types. split (',')
Pattern = '\. (' + '|'. join ([t. strip () for t in types]) + ') $ ';
Return re. search (pattern, filename, re. I )! = None
# Web request handler
# N lines of code are omitted here
Why does this repetitive work occur?
1. The front-end will never be trusted;
2. The frontend and backend use different programming languages;
What are the costs of such redundancy?
1. Modify the business logic twice. If the docx file type is suddenly not supported, you must modify the javascript code and python code at the same time.
2. added the cost of ensuring that the javascript code and python code business logic are consistent. You need to write two types of tests respectively, and the unit test is doubled.
Nodejs era: DRY
Use nodejs no more DRY!
One piece of code, and the front-end and back-end run simultaneouslyCopy codeThe Code is as follows: // constant. js
(Function (exports ){
Exports. RESUME_FILETYPES = 'doc, docx, pdf, txt, wps, odf, md, png, gif, jpg ';
}) (Function (){
If (typeof exports = 'undefined '){
Window. constant = {};
Return window. constant;
} Else {
Return exports;
}
})());
// Util. js
(Function (exports ){
/**
* Remove white spaces at both ends of the string.
*
* @ Return {String}
* @ Api public
*/
String. prototype. trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
};
/**
* Determine whether a custom file is used.
*
* @ Param {String} filename
* @ Param {String} types. Multiple types are separated by commas (,), such as doc, docx, and txt.
* @ Return {Boolean} true or false
* @ Api public
*/
Var is_filetype = exports. is_filetype = function (filename, types ){
Types = types. split (',');
Var pattern = '\.(';
For (var I = 0; I <types. length; I ++ ){
If (0! = I ){
Pattern + = '| ';
}
Pattern + = types [I]. trim ();
}
Pattern + = ') $ ';
Return new RegExp (pattern, 'I'). test (filename );
};
}) (Function (){
If (typeof exports = 'undefined '){
Window. util = {};
Return window. util;
} Else {
Return exports;
}
})());
Front endCopy codeThe Code is as follows: <script src = "/js/util. js"> </script>
<Script src = "/js/constant. js"> </script>
// N lines of code are omitted here
If (! Util. is_filetype ($ ('# uploadfile'). val (), constant. RESUME_FILETYPES )){
Can_submit = false; // upload is not allowed
$ ('# Uploadfile'). val ('');
Alert ('upload only allowed: '+ constant. RESUME_FILETYPES );
}
// N lines of code are omitted here
BackendCopy codeThe Code is as follows: var util = require ('./public/js/util. js '),
Constant = require ('./public/js/constant. js ');
App. post ('/resume/upload/: job_id', function (req, res, next ){
Req. form. complete (function (err, fields, files ){
If (! Util. is_filetype (filepath, constant. RESUME_FILETYPES )){
// Because the client has already made a judgment, this situation is caused by malicious uploads, directly prompting
Res. send ('file format error: '+ filepath
+ ', Upload' + constant. RESUME_FILETYPES + 'formatted file ');
Return;
}
// Save file...
// N lines of code are omitted here
});
});
Wow, no redundancy! Done
Other common scenarios
Constant Definition
Various useful tool modules, such as string operations