Skip to content

Instantly share code, notes, and snippets.

@pokorson
Created November 22, 2019 17:38
Show Gist options
  • Save pokorson/eb40c2aa4faac4cedb2d50d5efaa6264 to your computer and use it in GitHub Desktop.
Save pokorson/eb40c2aa4faac4cedb2d50d5efaa6264 to your computer and use it in GitHub Desktop.
const express = require('express')
const bodyParser = require('body-parser')
const uuidv1 = require('uuid/v1');
const cors = require('cors')
const app = express()
const port = 3000
app.use(cors())
const jsonParser = bodyParser.json()
const messages = []
const users = []
function sendMessage(messageBody, login) {
const message = {
body: messageBody,
sentBy: login
}
messages.push(message)
}
function loginUser(login){
const user = {
login,
uuid: uuidv1()
}
users.push(user);
return user;
}
app.post('/login', jsonParser, (req, res) => {
const user = loginUser(req.body.login);
console.log(user);
users.push(user);
return res.json(user);
})
app.post('/sendMessage', jsonParser, (req, res) => {
const message = sendMessage(reg.body.message, reg.body.message)
})
app.get('/messages', (req, res) => {
return res.json(messages)
})
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment