App entry point init
parent
c1db8da3a9
commit
ef4165d746
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);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue