2017年4月11日 星期二

[Node.js] Use function in other file


The code of string.js like below
function randomString(length) {
    var result = '';           
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for (var i = length; i > 0; --i)
        result += chars[Math.floor(Math.random() * chars.length)];
    return result;             
} 

var methods = {
        currentString: function() {     
            rString = randomString(12);     
            console.log("Your random route is: localhost:3000/" + rString)
            return rString;    

        }                      
};

module.exports = methods;


main.js want use function which in currentstring.js
// Insert following code into the place which you want

        var SendPage = require("./string.js");
        var WebString = SendPage.currentString();
Reference:

0 意見:

張貼留言