'use strict';

const express = require('express');
const app = express.Router();
const messagesApi = require('./messages.js');
const pushApi = require('./push.js');
app.use(require('body-parser').json());

function errorResponse(res, e) {
   res.status(500);
   res.json({
	   error: e.message
   });
}

app.get('/poke', function (req,res) {

	if (!req.query.username) {
		res.status(500);
		return res.json({
			error: 'No username param'
		});
	}
	pushApi(req.query.username)
	.then(() => {
		res.json({success: true})
	})
	.catch(e => errorResponse(res, e));
});

app.post('/send-message', function (req,res) {

	console.log(req.body);

	if (!req.body.username) {
		res.status(500);
		return res.json({
			error: 'No username param'
		});
	}

	if (!req.body.message) {
		res.status(500);
		return res.json({
			error: 'No message param'
		});
	}

	let user = 'Anonymous';
	if (req.user && req.user.username) {
		user = req.user.username;
	}
	messagesApi.pushMessage(req.body.username, JSON.stringify(
		{
			type: 'message',
			message: req.body.message,
			from: user,
			timestamp: Date.now()
		}
	))
	.then(m => {
		pushApi(req.body.username)
		res.json({
			success: true,
			noOfMessages: m
		});
	})
	.catch(e => errorResponse(res, e));
});

app.all('/get-messages', require('connect-ensure-login').ensureLoggedIn('/auth/twitter'), function (req,res) {

	if (!req.user) {
		res.status(500);
		return res.json({
			error: 'Not logged in'
		});
	}
	messagesApi
	.readMessages(req.user.username, req.query.start, req.query.amount)
	.then(m => {
		res.json(m.map(str => {
			try {
				return JSON.parse(str);
			} catch (e) {
				return false;
			}
		}));
	})
	.then(m => m.filter(l => typeof l === 'object'))
	.catch(e => errorResponse(res, e));
});

module.exports = app;

@@ -162,16 +162,19 @@
 ');%0A
-app.use(
+const bp = 
 requ
@@ -195,16 +195,8 @@
 er')
-.json())
 ;%0A%0Af
@@ -292,16 +292,37 @@
  %7D);%0A%7D%0A%0A
+app.use(bp.json());%0A%0A
 app.get(
@@ -628,33 +628,8 @@
  %7B%0A%0A
-%09console.log(req.body);%0A%0A
 %09if 

Read body