App entry point init

pull/6/head
sundowndev 2018-08-30 14:21:40 +02:00
parent c1db8da3a9
commit ef4165d746
1 changed files with 56 additions and 7 deletions

View File

@ -1,13 +1,62 @@
"use strict";
import ApiClient from './src/ApiClient';
import JsonResponse from './src/JsonResponse';
const express = require('express');
const bodyParser = require('body-parser');
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, () => {
console.log('We are live on ' + port);