Current situation: redundant
In web development, do we often use different programming languages to implement the same functionality?
For example, a file upload function requires file format restrictions on uploaded files. We usually use the suffix name as a limitation.
front end
For user experience, the file selected by the user will be judged on the page, so that the user can upload it only if it is legal.
function is_filetype(filename, types) {
types = (',');
var pattern = '\.(';
for(var i=0; i<; 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; // Uploading is not allowed
$('#uploadfile').val('');
alert('Uploads are only allowed: ' + constant.RESUME_FILETYPES);
}
// N lines of code are omitted here
rear end
Due to concerns about malicious uploads, it is inevitable that the files uploaded by the user need to be re-judged. So I wrote a logic to judge file suffix in python
import re
def is_filetype(filename, types):
types = (',')
pattern = '\.(' + '|'.join([() for t in types]) + ')$';
return (pattern, filename, ) != None
# web request handler
# N lines of code are omitted here
What is the reason for this repetitive work?
1. The front-end is never trustworthy;
2. The front-end and back-end use different programming languages;
What will this redundancy cost?
1. Modify the business logic and repeat it twice: if you suddenly find that the docx file type is less supported, you need to modify the javascript code and python code at the same time
2. Increase the cost of ensuring that the business logic of javascript code and python code are consistent. You need to write two types of tests separately, and the unit test runs twice as much.
nodejs era: DRY
//
(function(exports){
exports.RESUME_FILETYPES = 'doc,docx,pdf,txt,wps,odf,md,png,gif,jpg';
})( (function(){
if(typeof exports === 'undefined') {
= {};
return ;
} else {
return exports;
}
})() );
//
(function(exports){
/**
* Remove whitespace characters at both ends of the string
*
* @return {String}
* @api public
*/
= function(){
return (/(^\s*)|(\s*$)/g, "");
};
/**
* Determine whether a custom type of file
*
* @param {String}filename
* @param {String}types, multiple types are separated by numbers, such as doc,docx,txt
* @return {Boolean} true or false
* @api public
*/
var is_filetype = exports.is_filetype = function(filename, types) {
types = (',');
var pattern = '\.(';
for(var i=0; i<; i++) {
if(0 != i) {
pattern += '|';
}
pattern += types[i].trim();
}
pattern += ')$';
return new RegExp(pattern, 'i').test(filename);
};
})( (function(){
if(typeof exports === 'undefined') {
= {};
return ;
} else {
return exports;
}
})() );
front end
<script src="/js/"></script>
<script src="/js/"></script>
// N lines of code are omitted here
if(!util.is_filetype($('#uploadfile').val(), constant.RESUME_FILETYPES)){
can_submit = false; // Uploading is not allowed
$('#uploadfile').val('');
alert('Uploads are only allowed: ' + constant.RESUME_FILETYPES);
}
// N lines of code are omitted here
rear end
var util = require('./public/js/'),
constant = require('./public/js/');
('/resume/upload/:job_id', function(req, res, next){
(function(err, fields, files){
if(!util.is_filetype(filepath, constant.RESUME_FILETYPES)) {
// Since the client has made a judgment, this situation is maliciously uploaded. It is directly prompted
('File format error: ' + filepath
+ ' , please upload ' + constant.RESUME_FILETYPES + 'File in format');
return;
}
// save file ...
// N lines of code are omitted here
});
});
wow, there is no redundancy! done
Other common scenarios
Constant definition
Various useful tool modules, such as string operations
In web development, do we often use different programming languages to implement the same functionality?
For example, a file upload function requires file format restrictions on uploaded files. We usually use the suffix name as a limitation.
front end
For user experience, the file selected by the user will be judged on the page, so that the user can upload it only if it is legal.
Copy the codeThe code is as follows:
function is_filetype(filename, types) {
types = (',');
var pattern = '\.(';
for(var i=0; i<; 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; // Uploading is not allowed
$('#uploadfile').val('');
alert('Uploads are only allowed: ' + constant.RESUME_FILETYPES);
}
// N lines of code are omitted here
rear end
Due to concerns about malicious uploads, it is inevitable that the files uploaded by the user need to be re-judged. So I wrote a logic to judge file suffix in python
Copy the codeThe code is as follows:
import re
def is_filetype(filename, types):
types = (',')
pattern = '\.(' + '|'.join([() for t in types]) + ')$';
return (pattern, filename, ) != None
# web request handler
# N lines of code are omitted here
What is the reason for this repetitive work?
1. The front-end is never trustworthy;
2. The front-end and back-end use different programming languages;
What will this redundancy cost?
1. Modify the business logic and repeat it twice: if you suddenly find that the docx file type is less supported, you need to modify the javascript code and python code at the same time
2. Increase the cost of ensuring that the business logic of javascript code and python code are consistent. You need to write two types of tests separately, and the unit test runs twice as much.
nodejs era: DRY
Use nodejs no more DRY !
One copy of code, the front-end and back-end run at the same time
Copy the codeThe code is as follows:
//
(function(exports){
exports.RESUME_FILETYPES = 'doc,docx,pdf,txt,wps,odf,md,png,gif,jpg';
})( (function(){
if(typeof exports === 'undefined') {
= {};
return ;
} else {
return exports;
}
})() );
//
(function(exports){
/**
* Remove whitespace characters at both ends of the string
*
* @return {String}
* @api public
*/
= function(){
return (/(^\s*)|(\s*$)/g, "");
};
/**
* Determine whether a custom type of file
*
* @param {String}filename
* @param {String}types, multiple types are separated by numbers, such as doc,docx,txt
* @return {Boolean} true or false
* @api public
*/
var is_filetype = exports.is_filetype = function(filename, types) {
types = (',');
var pattern = '\.(';
for(var i=0; i<; i++) {
if(0 != i) {
pattern += '|';
}
pattern += types[i].trim();
}
pattern += ')$';
return new RegExp(pattern, 'i').test(filename);
};
})( (function(){
if(typeof exports === 'undefined') {
= {};
return ;
} else {
return exports;
}
})() );
front end
Copy the codeThe code is as follows:
<script src="/js/"></script>
<script src="/js/"></script>
// N lines of code are omitted here
if(!util.is_filetype($('#uploadfile').val(), constant.RESUME_FILETYPES)){
can_submit = false; // Uploading is not allowed
$('#uploadfile').val('');
alert('Uploads are only allowed: ' + constant.RESUME_FILETYPES);
}
// N lines of code are omitted here
rear end
Copy the codeThe code is as follows:
var util = require('./public/js/'),
constant = require('./public/js/');
('/resume/upload/:job_id', function(req, res, next){
(function(err, fields, files){
if(!util.is_filetype(filepath, constant.RESUME_FILETYPES)) {
// Since the client has made a judgment, this situation is maliciously uploaded. It is directly prompted
('File format error: ' + filepath
+ ' , please upload ' + constant.RESUME_FILETYPES + 'File in format');
return;
}
// save file ...
// N lines of code are omitted here
});
});
wow, there is no redundancy! done
Other common scenarios
Constant definition
Various useful tool modules, such as string operations