diff --git a/index.js b/index.js index 83df404..862863f 100644 --- a/index.js +++ b/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 = '
' + p.body + '
'; + 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); });