commit
ba48dd1ca2
|
@ -1 +1,3 @@
|
||||||
|
.idea
|
||||||
node_modules
|
node_modules
|
||||||
|
dist
|
|
@ -14,6 +14,7 @@ $ docker-compose build
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
$ npm install
|
$ npm install
|
||||||
|
$ npm run build
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
@ -21,7 +22,7 @@ $ npm install
|
||||||
Launch server
|
Launch server
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
$ node index.js
|
$ npm run start
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
Launch application with Docker
|
Launch application with Docker
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
const postsRoutes = require('./posts');
|
|
||||||
|
|
||||||
module.exports = function(app, db) {
|
|
||||||
postsRoutes(app);
|
|
||||||
};
|
|
|
@ -1,6 +0,0 @@
|
||||||
module.exports = function(app) {
|
|
||||||
app.get('/posts', (req, res) => {
|
|
||||||
// You'll create your note here.
|
|
||||||
res.send('Hello')
|
|
||||||
});
|
|
||||||
}
|
|
55
index.js
55
index.js
|
@ -1,13 +1,62 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
import ApiClient from './src/ApiClient';
|
||||||
|
import JsonResponse from './src/JsonResponse';
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
const api = new ApiClient();
|
||||||
|
const json = new JsonResponse();
|
||||||
|
|
||||||
const port = 8000;
|
/**
|
||||||
|
* Config
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
//require functions
|
/**
|
||||||
|
* Routes
|
||||||
|
*/
|
||||||
|
app.use(function (req, res, next) {
|
||||||
|
// Default api domain
|
||||||
|
api.setApiUrl('https://jsonplaceholder.typicode.com');
|
||||||
|
|
||||||
//routes
|
json.init(app, res);
|
||||||
|
|
||||||
|
next()
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
return json.send(res, 200, {data: []});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/posts', (req, res) => {
|
||||||
|
let posts = api.findAllPosts();
|
||||||
|
|
||||||
|
posts.forEach(function (p) {
|
||||||
|
let user = api.findUserById(p.userId);
|
||||||
|
|
||||||
|
p.title = '<h1>' + p.title + '</h1>';
|
||||||
|
p.body = '<p>' + p.body + '</p>';
|
||||||
|
p.user = {
|
||||||
|
"id": user.id,
|
||||||
|
"firstname": user.name,
|
||||||
|
"lastname": user.name,
|
||||||
|
"email": user.email,
|
||||||
|
"comments_count": 3,
|
||||||
|
"pos": user.geo,
|
||||||
|
"_links": {
|
||||||
|
"posts": "/posts?user=" + user.id
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
delete p.userId;
|
||||||
|
});
|
||||||
|
|
||||||
|
return json.send(res, 200, {data: posts});
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log('We are live on ' + port);
|
console.log('We are live on ' + port);
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
|
@ -4,7 +4,9 @@
|
||||||
"description": "## Exercice",
|
"description": "## Exercice",
|
||||||
"main": "app.js",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
|
||||||
|
"start": "npm run build && node dist/index.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -18,10 +20,15 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/Sundowndev/interview-v2#readme",
|
"homepage": "https://github.com/Sundowndev/interview-v2#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"async": "^2.6.1",
|
||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.18.3",
|
||||||
"express": "^4.16.3"
|
"express": "^4.16.3",
|
||||||
|
"request": "^2.88.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^1.18.3"
|
"babel-cli": "^6.26.0",
|
||||||
|
"babel-preset-es2015": "^6.24.1",
|
||||||
|
"nodemon": "^1.18.3",
|
||||||
|
"rimraf": "^2.6.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const request = require('request');
|
||||||
|
const async = require('async');
|
||||||
|
|
||||||
|
export default function ApiClient() {
|
||||||
|
this.ApiUrl = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
ApiClient.prototype.setApiUrl = function (url) {
|
||||||
|
ApiClient.ApiUrl = url;
|
||||||
|
};
|
||||||
|
|
||||||
|
ApiClient.prototype.sendRequest = function (method, resource, data, callback) {
|
||||||
|
async.parallel([
|
||||||
|
function () {
|
||||||
|
request.get({
|
||||||
|
"headers": {"content-type": "application/json"},
|
||||||
|
"url": ApiClient.ApiUrl + resource,
|
||||||
|
"data": data
|
||||||
|
}, (error, response, body) => {
|
||||||
|
if(error) { console.log(err); callback(true); return; }
|
||||||
|
let obj = JSON.parse(body);
|
||||||
|
callback(false, obj);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all posts
|
||||||
|
*/
|
||||||
|
ApiClient.prototype.findAllPosts = function () {
|
||||||
|
console.log(this.sendRequest('GET', '/posts', []));
|
||||||
|
|
||||||
|
this.sendRequest('GET', '/posts', [], function (result) {
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find posts by user id
|
||||||
|
*/
|
||||||
|
ApiClient.prototype.findPostsByUser = function (userId) {
|
||||||
|
let posts = this.sendRequest('GET', '/posts', []);
|
||||||
|
|
||||||
|
let postsOfUser = [];
|
||||||
|
|
||||||
|
posts.forEach(function (p) {
|
||||||
|
if (p.userId === userId) {
|
||||||
|
postsOfUser.push(p);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return postsOfUser;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find user by id
|
||||||
|
*/
|
||||||
|
ApiClient.prototype.findUserById = function (userId) {
|
||||||
|
return this.sendRequest('GET', '/users/'+userId, []);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find comments by user id
|
||||||
|
*/
|
||||||
|
ApiClient.prototype.findCommentsByUserId = function () {
|
||||||
|
return this.sendRequest('GET', '/comments', []);
|
||||||
|
};
|
|
@ -0,0 +1,19 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
export default function JsonResponse() {
|
||||||
|
this.httpCode = 200;
|
||||||
|
this.headers = {'Content-Type': 'application/json'};
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonResponse.prototype.init = function (app, res) {
|
||||||
|
// Enable json prettify
|
||||||
|
app.set('json spaces', 2);
|
||||||
|
|
||||||
|
// Set response headers
|
||||||
|
res.setHeader('Accept', 'application/json');
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
};
|
||||||
|
|
||||||
|
JsonResponse.prototype.send = function (res, code, response) {
|
||||||
|
return res.json(response);
|
||||||
|
};
|
Loading…
Reference in New Issue