repl.it
@Kognise/

PSTY POC Backend

Nodejs

No description

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const uid = require('uid-promise')

const store = {}

express()
  .use(bodyParser.json())
  .use(cors())
  .post('/new', async (req, res) => {
    const id = await uid(6)
    const token = await uid(24)
    store[id] = {
      token,
      content: req.body.content
    }
    res.json({ id, token })
  })
  .post('/update', (req, res) => {
    if (store[req.body.id].token === req.body.token) {
      store[req.body.id].content = req.body.content
      res.sendStatus(200)
    } else {
      res.sendStatus(403)
    }
  })
  .get('/get/:id', (req, res) => {
    res.contentType('text/plain')
    res.send(store[req.params.id].content)
  })
  .listen(() => console.log('ready'))
node v10.16.0