Nodejs suffix name judgment limit code _javascript tips

Source: Internet
Author: User
Status Quo: Redundancy
Do we often use different programming languages to implement the same functionality in Web development?
such as a file upload function, you need to upload files to file format restrictions. We usually use the suffix name to make the restriction.

Front
For the user experience, will be in the page of the user selected files to judge, legitimate to allow users to upload.
Copy Code code 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 to new RegExp (pattern, ' I '). Test (filename);
};

n line of code omitted here
if (!is_filetype (' #uploadfile '). Val (), ' doc,pdf,txt,wps,odf,md,png,gif,jpg ') {
Can_submit = false; Not allowed to upload
$ (' #uploadfile '). Val (');
Alert (' Only allow upload: ' + constant. Resume_filetypes);
}
n line of code omitted here

Back end
Due to the fear of malicious uploads, it is unavoidable to need to re-upload the user's files to judge. So I wrote a logic to determine the file suffix in Python.
Copy Code code 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
# The N line of code is omitted here

The reason for this repetitive work is why?
1. Front end is never credible;
2. Front-End and back-end use of different programming languages;
What is the cost of such redundancy?
1. Modify the business logic, need to do 2 times: if suddenly found that less support for docx file types, you need to modify both JavaScript code and Python code
2. Increase the cost of ensuring consistency between JavaScript code and Python code business logic. You need to write 2 different tests, unit test runs one more times.
Nodejs Times: DRY

Use Nodejs no more DRY!

A code that runs at the back end of the front-end
Copy Code code 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 whitespace characters at both ends of the string
*
* @return {String}
* @api Public
*/
String.prototype.trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
};

/**
* Determine whether a custom type of file
*
* @param {String}filename
* @param {string}types, multiple types use, number delimited, such as Doc,docx,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 to new RegExp (pattern, ' I '). Test (filename);
};

}) ((function () {
if (typeof exports = = ' undefined ') {
Window.util = {};
return window.util;
} else {
return exports;
}
})() );

Front
Copy Code code as follows:

<script src= "/js/util.js" ></script>
<script src= "/js/constant.js" ></script>

n line of code omitted here
if (!util.is_filetype (' #uploadfile '). Val (), Constant. Resume_filetypes)) {
Can_submit = false; Not allowed to upload
$ (' #uploadfile '). Val (');
Alert (' Only allow upload: ' + constant. Resume_filetypes);
}
n line of code omitted here

Back end
Copy Code code 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 made the judgment, so this kind of situation is maliciously uploaded, direct prompt
Res.send (' File format error: ' + filepath
+ ', please upload ' + constant. Resume_filetypes + ' format file ');
Return
}
Save File ...
n line of code omitted here
});
});

Wow, there's no redundancy! Done
Other common scenarios
Constant definition
A variety of useful tool modules, such as string manipulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.