commit
ba48dd1ca2
|
@ -1 +1,3 @@
|
|||
.idea
|
||||
node_modules
|
||||
dist
|
|
@ -14,6 +14,7 @@ $ docker-compose build
|
|||
|
||||
~~~
|
||||
$ npm install
|
||||
$ npm run build
|
||||
~~~
|
||||
|
||||
### Usage
|
||||
|
@ -21,7 +22,7 @@ $ npm install
|
|||
Launch server
|
||||
|
||||
~~~
|
||||
$ node index.js
|
||||
$ npm run start
|
||||
~~~
|
||||
|
||||
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')
|
||||
});
|
||||
}
|
63
index.js
63
index.js
|
@ -1,14 +1,63 @@
|
|||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
"use strict";
|
||||
|
||||
const app = express();
|
||||
import ApiClient from './src/ApiClient';
|
||||
import JsonResponse from './src/JsonResponse';
|
||||
|
||||
const port = 8000;
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
//require functions
|
||||
const app = express();
|
||||
const api = new ApiClient();
|
||||
const json = new JsonResponse();
|
||||
|
||||
//routes
|
||||
/**
|
||||
* Config
|
||||
* @type {number}
|
||||
*/
|
||||
const port = 3000;
|
||||
|
||||
/**
|
||||
* Routes
|
||||
*/
|
||||
app.use(function (req, res, next) {
|
||||
// Default api domain
|
||||
api.setApiUrl('https://jsonplaceholder.typicode.com');
|
||||
|
||||
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, () => {
|
||||
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",
|
||||
"main": "app.js",
|
||||
"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": {
|
||||
"type": "git",
|
||||
|
@ -18,10 +20,15 @@
|
|||
},
|
||||
"homepage": "https://github.com/Sundowndev/interview-v2#readme",
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"body-parser": "^1.18.3",
|
||||
"express": "^4.16.3"
|
||||
"express": "^4.16.3",
|
||||
"request": "^2.88.0"
|
||||
},
|
||||
"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