Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ npm install
npm test
```

##安裝babel-cli

```
npm install -g babel-cli
```

開起檔案
```
babel-node <檔案名稱.js>
```

##problem

如有遇到下圖的問題
Expand Down
Binary file modified database.db
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"watchPost": "sqlite3 database.db 'select * from posts'"
},
"devDependencies": {
"babel-cli": "*",
"babel-cli": "^6.10.1",
"babel-core": "^6.8.0",
"babel-eslint": "^6.0.4",
"babel-plugin-syntax-async-functions": "^6.8.0",
Expand Down
4 changes: 2 additions & 2 deletions src/facebook/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class FacebookHelper {
async getFriends() {
try {
let result = await new Promise((resolve, reject) => {
this.FB.api(`${this.userId}/friends`, function(res, error) {
this.FB.api(`${this.userId}/friends?fields=id,name`, function(res, error) {
if(error) reject(error);
resolve(res.data);
});
Expand All @@ -44,4 +44,4 @@ export default class FacebookHelper {
}
}

}
};
16 changes: 16 additions & 0 deletions src/models/friend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

module.exports = (sequelize, DataTypes) => {
var Friend = sequelize.define('Friend', {
name: DataTypes.STRING,
email: DataTypes.STRING,
fbId: DataTypes.STRING,
}, {
classMethods: {
associate: (models) => {
}
}
});

return Friend;
};
5 changes: 2 additions & 3 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

module.exports = (sequelize, DataTypes) => {
var User = sequelize.define('User', {
username: DataTypes.STRING,
name: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
age: DataTypes.INTEGER,
fbId: DataTypes.STRING,
}, {
classMethods: {
associate: (models) => {
Expand Down
6 changes: 6 additions & 0 deletions src/server/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function add(x,y){
console.log("add ok");
return x + y;
}

exports.add = add;
10 changes: 8 additions & 2 deletions src/server/requestHandlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export function start() {
export function start(response) {
console.log("Request handler 'start' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Start");
response.end();
}
export function upload() {
export function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
7 changes: 5 additions & 2 deletions src/server/router.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

export default function route(handle, pathname) {
export default function route(handle, pathname, response) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname]();
handle[pathname](response);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 not found");
response.end();
}
}
140 changes: 140 additions & 0 deletions src/server/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
var Http = require('http'),
Router = require('router'),
server,
router;

router = new Router();

var BodyParser = require('body-parser');


server = Http.createServer(function(request, response) {
router(request, response, function(error) {
if (!error) {
response.writeHead(404);
} else {
// Handle errors
console.log(error.message, error.stack);
response.writeHead(400);
}
response.end('RESTful API Server is running!\n');
});
});

server.listen(3000, function() {
console.log('Listening on port 3000');
});

var counter = 0,
todoList = {};

router.use(BodyParser.text());

function createItem(request, response) {
var id = counter += 1,
item = request.body;

console.log('Create item', id, item);
todoList[id] = item;
response.writeHead(201, {
'Content-Type': 'text/plain',
'Location': '/todo/' + id
});
response.end(item);
}
router.post('/todo', createItem);

function readItem(request, response) {
var id = request.params.id,
item = todoList[id];

if (typeof item !== 'string') {
console.log('Item not found', id);
response.writeHead(404);
response.end('\n');
return;
}

console.log('Read item', id, item);

response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end(item);
}
router.get('/todo/:id', readItem);

function deleteItem(request, response) {
var id = request.params.id;

if (typeof todoList[id] !== 'string') {
console.log('Item not found', id);
response.writeHead(404);
response.end('\n');
return;
}

console.log('Delete item', id);

todoList[id] = undefined;
response.writeHead(204, {
'Content-Type': 'text/plain'
});
response.end('');
}
router.delete('/todo/:id', deleteItem);

function readList(request, response) {
var item,
itemList = [],
listString;

for (id in todoList) {
if (!todoList.hasOwnProperty(id)) {
continue;
}
item = todoList[id];

if (typeof item !== 'string') {
continue;
}

itemList.push(item);
}

console.log('Read List: \n', JSON.stringify(
itemList,
null,
' '
));

listString = itemList.join('\n');

response.writeHead(200, {
'Content-Type': 'tet/plain'
});
response.end(listString);
}
router.get('/todo', readList);

function updateItem(request, response) {
var id = request.params.id,
item = request.body;

if (typeof todoList[id] !== 'string') {
console.log('Item not found', id);
response.writeHead(404);
response.end('\n');
return;
}

console.log('Update item', id, item);

todoList[id] = item;
response.writeHead(201, {
'Content-Type': 'text/plain',
'Location': '/todo/' + id
});
response.end(item);
}
router.put('/todo/:id', updateItem);
41 changes: 39 additions & 2 deletions test/unit/database/task1.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import task1_initModel from '../../../src/database/task1';

describe('database level 1', () => {
describe.skip('database level 1', () => {
let models = null;
beforeEach(async (done) => {
try {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('database level 1', () => {
});


describe('database task1 find', () => {
describe.skip('database task1 find', () => {

let models = null;
beforeEach(async (done) => {
Expand Down Expand Up @@ -198,3 +198,40 @@ describe('database task1 find', () => {
});

});

describe('model define' , () => {
let models = null;
beforeEach(async (done) => {
try {
models = await task1_initModel()
done()
} catch (e) {
done(e)
}
});

it('model defined', async (done) => {
try {
let addUser = {name: 'test', fbId: 'test', email: 'test@mail.com' };
let result = {};
result = await models.User.create(addUser)
result.toJSON().should.has.keys(
'id',
'name',
'fbId',
'email',
'createdAt',
'updatedAt'
);
result.name.should.be.eq('test');
result.name.should.be.string;
result.fbId.should.be.string;
result.email.should.be.string;
done();
} catch (e) {
done(e);
}
});


})
Loading