Created
February 19, 2018 01:45
-
-
Save Evshved/6e37561b7683246fa1dbe8c4e632bfea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const webpack = require('webpack'); | |
| const koa = require('koa'); | |
| const koaWebpack = require('koa-webpack'); | |
| const webpackConfig = require('../web/client/webpack.config'); | |
| const router = require('koa-router'); | |
| const serve = require('koa-static'); | |
| const path = require('path'); | |
| const bodyParser = require('koa-bodyparser'); | |
| const session = require('koa-session'); | |
| const Joi = require('joi'); | |
| const schema = ({ | |
| firstName: Joi.string().trim().regex(/^[A-Za-zА-Яа-яЁё]+$/g).required(), | |
| secondName: Joi.string().regex(/^[A-Za-zА-Яа-яЁё]+$/g).required(), | |
| description: Joi.string().min(3).required(), | |
| mark: Joi.number(), | |
| }); | |
| const app = new koa(); | |
| const koaRouter = new router(); | |
| app.keys = ['secret', 'key']; | |
| app.use(bodyParser()); | |
| app | |
| .use(koaRouter.routes()) | |
| .use(koaRouter.allowedMethods()); | |
| app.use(serve(path.resolve(__dirname, '../web/client/'))); | |
| app.use(session(app)); | |
| app.use((ctx,next) => { | |
| if (ctx.path === '/public') { | |
| let n = ctx.session.views || 0; | |
| ctx.session.views = ++n; | |
| } | |
| next(); | |
| }); | |
| app.use(koaWebpack({ | |
| compiler: webpack(webpackConfig), | |
| dev: { | |
| publicPath: webpackConfig.output.publicPath, | |
| }, | |
| })); | |
| const messages = []; | |
| koaRouter.post('/message', ctx => { | |
| const result = Joi.validate(ctx.request.body, schema); | |
| if (result.error !== null) { | |
| ctx.body = result.error; | |
| ctx.status = 400; | |
| } | |
| else { | |
| messages.push(ctx.request.body); | |
| ctx.status = 200; | |
| } | |
| }); | |
| koaRouter.get('/messageList', (ctx) => { | |
| ctx.body = messages; | |
| }); | |
| koaRouter.get('/showVisits', (ctx) => ctx.body = ctx.session.views); | |
| app.listen(3001, () => { | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| const showList = () => { | |
| axios.get('/messageList') | |
| .then((response) => { | |
| let body = response.data.map((message) => { | |
| return ` | |
| <div class="review"> | |
| <div class="name"> | |
| <h1>${message.secondName} ${message.firstName}</h1> | |
| </div> | |
| <div class="description"><h2>${message.description.toString()}</h2></div> | |
| <div class="rating"><h3>${message.mark}</h3></div> | |
| <hr> | |
| </div>`; | |
| }).join(''); | |
| document.querySelector('#messageList').innerHTML = body; | |
| }) | |
| }; | |
| showList() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| const showVisits = () => { | |
| axios.get('/showVisits') | |
| .then((response) => { | |
| document.querySelector('#visitsCount').innerHTML = `<div>Вы посетили эту страницу ${response.data} раз</div>`; | |
| }) | |
| }; | |
| showVisits(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment