2017-10-11 28 views
6

में विधि के साथ सभी जेएस फाइल क्रियाएं कैसे प्राप्त करें मैं सभी विधि सेटिंग्स के साथ सभी .js फ़ाइल की क्रिया/मार्ग लाने के लिए चाहता हूं। असल में मैं एक ऐसा फ़ंक्शन बनाना चाहता हूं जो मुझे सभी कार्यवाही और विधियों को वापस कर दे।नोड जेएस

router.get('/this/is/action', function (req, res, next) { 
    // This action is in login.js 
} 

router.post('/another/action1', function (req, res, next) { 
    // This action is in signup.js 
} 

तो, इस प्रकार के मामले में मेरे फ़ंक्शन को मुझे प्रतिक्रिया देना होगा जैसा कि नीचे जैसा दिखता है।

{ 
    "data":[ 
     { 
      "url":"/this/is/action", 
      "method":"get" 
     } 
     { 
      "url":"/another/action1", 
      "method":"post" 
     } 
    ] 
} 
+0

मैंने रूट.स्टैक का उपयोग किया है, लेकिन यह मुझे केवल फाइलों की क्रिया और विधि देगा। –

उत्तर

0

आप उपयोग कर रहे हैं व्यक्त आप एक्सप्रेस के dougwilson निर्माता द्वारा बनाई निम्नलिखित स्निपेट का उपयोग कर सकते हैं।

function print (path, layer) { 
    if (layer.route) { 
    layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path)))) 
    } else if (layer.name === 'router' && layer.handle.stack) { 
    layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp)))) 
    } else if (layer.method) { 
    console.log('%s /%s', 
     layer.method.toUpperCase(), 
     path.concat(split(layer.regexp)).filter(Boolean).join('/')) 
    } 
} 

function split (thing) { 
    if (typeof thing === 'string') { 
    return thing.split('/') 
    } else if (thing.fast_slash) { 
    return '' 
    } else { 
    var match = thing.toString() 
     .replace('\\/?', '') 
     .replace('(?=\\/|$)', '$') 
     .match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//) 
    return match 
     ? match[1].replace(/\\(.)/g, '$1').split('/') 
     : '<complex:' + thing.toString() + '>' 
    } 
} 

app._router.stack.forEach(print.bind(null, [])) 
+0

आपके उत्तर के लिए धन्यवाद। मैं इसे लागू करने की कोशिश कर रहा हूं लेकिन मुझे लाइन 13 में त्रुटि मिली है। सिंटेक्स त्रुटि: अनपेक्षित टोकन) –

+0

मुझे कुछ सिंटैक्स त्रुटियां मिलीं, अब इसे काम करना चाहिए। –

+0

धन्यवाद यह मेरे लिए ठीक काम है !!! :) –