1
我想創建一個節點模塊來抓取一些帖子,但我得到一個未定義的錯誤。節點模塊導出返回未定義
Index.js
var request = require('request');
function getPosts() {
var options = {
url: 'https://myapi.com/posts.json',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
return JSON.parse(body);
}
}
request(options, callback);
}
exports.posts = getPosts;
測試/ index.js
var should = require('chai').should(),
myModule = require('../index');
describe('Posts call', function() {
it('should return posts', function() {
myModule.posts().should.equal(100);
});
});
我缺少什麼?
'getPosts'不返回任何東西 – naomik 2014-10-10 04:33:40