2015-10-02 43 views
0

我目前正在嘗試自己學習節點js,並且我對JavaScript也是全新的。當我嘗試閱讀並理解貓鼬時,我找到了這個代碼,沒有任何解釋。這是什麼字符串語法意味着在javascripts?

router.js

var url = require('url'); 
var fs = require('fs'); 
exports.get = function(req, res) { 
req.requrl = url.parse(req.url, true); 
var path = req.requrl.pathname; 
if (/\.(css)$/.test(path)){ 
    res.writeHead(200, {'Content-Type': 'text/css'}); 
    fs.readFile(__dirname + path, 'utf8', function (err, data){ 
    if (err) throw err; 
    res.write(data, 'utf8'); 
    res.end(); 
    }); 
} else { 
    if (path === '/' || path === '/home') { 
    require('./controllers/home-mongoose').get(req, res); 
    } else { 
    require('./controllers/404').get(req, res); 
    } 
} 
} 

首先,這是什麼exports.get?我有點明白exports = function functionA(){}意味着,當我可以做這樣的事情:

var router = require('path/router.js'); 
router.functionA(); 

但我沒有得到什麼,當你做exports.get這意味着什麼。

二,/\.(css)$/.test(path)。我沒有得到這種表達式語法,任何人都可以向我解釋?由於

回答

1

首先,這是什麼exports.get?我有點明白exports = function functionA(){}意味着,當我可以做這樣的事情:

var router = require('path/router.js'); 
router.functionA(); 

exports = function functionA(){}其實沒有你在想什麼。

在節點中,exports只是一個變量,最初是指Object。並且,該行將在該對象上設置一個get屬性/方法。

console.log(exports); // {} 

exports.get = function() {}; 

console.log(exports); // { get: function() {} } 

exports是指通常是指到模塊/文件時require()返回相同的對象的對象。

// `router` = `module.exports` from `./path/router.js` 
var router = require('./path/router'); 
router.get() 

欲瞭解更多有關exports,通過Modules documentation閱讀的Node.js


其次,/\.(css)$/.test(path)。我沒有得到這種表達式語法,任何人都可以向我解釋?

該表達式的第一部分/\.(css)$/RegExp literal

/ s是起始和結束分隔符,其行爲類似於字符串文字的引號。而且,在它們之間,\.(css)$是被定義的模式。

\.  // match a single period character 
     // escaped to disable the period's "any character" meaning 
(css) // match the 3 characters, "css", within a capture group 
$  // anchor the pattern to the end of the line/string 

表達也可以與RegExp構造函數寫爲:

new RegExp("\\.(css)$").test(path) // note the extra escape needed for the string 
+0

謝謝,這很清楚:) –

1

這不會做你認爲它的作用:

exports = function functionA(){} 

您必須設置module.exports,不exports。此外,你可以使用與:

var router = require('path/router.js'); 
router(); 

因爲你設置整個出口,而不是叫functionA屬性。

但是,您可以在exports僅有屬性:

exports.get = function functionA(){} 

這適用這種方式,因爲沒有安裝一個模塊在一個var exports = module.exports = {}module.exports是什麼在最後出口。

設置這樣的特性意味着,當你需要的模塊,您可以使用該屬性:

var router = require('path/router.js'); 
router.get(); 

你的第二個問題點,使用正則表達式的行。我建議對它們進行一些研究,或者詢問第二個問題,如果你想了解這個特定的問題,但是在Javascript中,你可以用斜槓(/)圍繞它們來編寫文字正則表達式,就像文字字符串被引號括起來一樣("/' )。

+0

謝謝。我得到第一個。我會在reg –

0

/\.(css)$/是一個正則表達式,它所做的是檢查給定的路徑/文件擴展名是否爲「.css」。

有在JavaScript兩種方式來構建一個正則表達式:

  1. 使用文字正則表達式:var reg = /[a-b]/;

  2. 或者使用構造函數var reg = new RegExp("[a-b]");

你可以閱讀更多關於正則表達式here

而關於第一個問題,因爲@Aaron杜福爾已經解釋過,我會存入他;)

+0

上做研究,謝謝。我會閱讀。雖然我沒有足夠的權力來upvote你awnser –

+0

我很高興幫助你:) –

0

1)exports對象就是你使用,使代碼提供給其他地方的代碼一個文件的對象。所以在你的情況下,exports.get只是定義一個名爲「get」的屬性,這個屬性恰好是一個函數。有關Node.js模塊格式的完整概述,請參閱see here

2)/\.(css)$/.test(path)相當於說「嘿,看看變量」路徑「,看它是否以」.css「結尾。欲瞭解更多有關.testsee here的信息。

+0

我明白了。儘管我沒有得到'測試'。通常,它應該是其他語言的其他方式,如'path.test(...)'。無論如何,非常感謝 –