Restful CRUD API with Node.js and Express

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I build a RESTful CRUD API with Node.js and Express. This application allows, for example, to create a table on the client side with the ability to add, update and remove data by ID.
I generate an array of random data for the availability of data when you start working on the client side with the table.
I will be grateful for tips on improving the style.



const express = require('express')
const bodyParser = require('body-parser')
const crypto = require("crypto");
const cors = require('cors')

const getId = () => crypto.randomBytes(16).toString("hex");
const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const nameList = ['Andrew', 'John', 'Sebastian', 'Owen', 'Luke', 'Anthony', 'Dylan']
const cityList = ['Riga', 'Berlin', 'Paris', 'Tokyo', 'London', 'Milan', 'Madrid']
const colorList = ['red', 'blue', 'green']
const getRandomStr = (str) => (str[getRandomInt(0, str.length)])

const app = express()

const names = [
id: getId(),
name: 'Li',
city: 'Tokyo',
color: 'blue',
answers: '10',
reputation: '1725'
]

const generateNames = () =>
for (let i=0; i<50; i++)
const newId = getId()
const newName = getRandomStr(nameList)
const newCity = getRandomStr(cityList)
const newColor = getRandomStr(colorList)
const newAnswers = getRandomInt(10, 20).toString()
const newReputation = getRandomInt(1500, 2000).toString()
names.push( id: newId, name: newName, city: newCity,
color: newColor, answers: newAnswers, reputation: newReputation )


generateNames()

app.use(bodyParser.json())

app.use(cors())

app.get('/names', (req, res) =>
res.json(names)
)

app.post('/names', (req, res) =>
const newId = getId()
const newName = req.body.name
const newCity = req.body.city
const newColor = req.body.color
const newAnswers = req.body.answers
const newReputation = req.body.reputation
names.push( id: newId, name: newName, city: newCity,
color: newColor, answers: newAnswers, reputation: newReputation )
res
.status(201)
.json( id: newId )
)

app.put('/names', (req, res) =>
const id = req.body
const indexToEdit = names.findIndex(obj => obj.id === id )
names[indexToEdit].name = req.body.name
names[indexToEdit].city = req.body.city
names[indexToEdit].color = req.body.color
names[indexToEdit].answers = req.body.answers
names[indexToEdit].reputation = req.body.reputation
const editName = names[indexToEdit].name
const editCity = names[indexToEdit].city
const editColor = names[indexToEdit].color
const editAnswers = names[indexToEdit].answers
const editReputation = names[indexToEdit].reputation
res
.status(200)
.json( name: editName, city: editCity,
color: editColor, answers: editAnswers, reputation: editReputation)
)

app.delete('/names', (req, res) =>
const id = req.body
const indexToRemove = names.findIndex(obj => obj.id === id )
names.splice(indexToRemove, 1)
res
.status(200)
.json( status: 'success')
)

app.listen(3000, () =>
console.log('listening on 3000')
)






share|improve this question

























    up vote
    4
    down vote

    favorite












    I build a RESTful CRUD API with Node.js and Express. This application allows, for example, to create a table on the client side with the ability to add, update and remove data by ID.
    I generate an array of random data for the availability of data when you start working on the client side with the table.
    I will be grateful for tips on improving the style.



    const express = require('express')
    const bodyParser = require('body-parser')
    const crypto = require("crypto");
    const cors = require('cors')

    const getId = () => crypto.randomBytes(16).toString("hex");
    const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
    const nameList = ['Andrew', 'John', 'Sebastian', 'Owen', 'Luke', 'Anthony', 'Dylan']
    const cityList = ['Riga', 'Berlin', 'Paris', 'Tokyo', 'London', 'Milan', 'Madrid']
    const colorList = ['red', 'blue', 'green']
    const getRandomStr = (str) => (str[getRandomInt(0, str.length)])

    const app = express()

    const names = [
    id: getId(),
    name: 'Li',
    city: 'Tokyo',
    color: 'blue',
    answers: '10',
    reputation: '1725'
    ]

    const generateNames = () =>
    for (let i=0; i<50; i++)
    const newId = getId()
    const newName = getRandomStr(nameList)
    const newCity = getRandomStr(cityList)
    const newColor = getRandomStr(colorList)
    const newAnswers = getRandomInt(10, 20).toString()
    const newReputation = getRandomInt(1500, 2000).toString()
    names.push( id: newId, name: newName, city: newCity,
    color: newColor, answers: newAnswers, reputation: newReputation )


    generateNames()

    app.use(bodyParser.json())

    app.use(cors())

    app.get('/names', (req, res) =>
    res.json(names)
    )

    app.post('/names', (req, res) =>
    const newId = getId()
    const newName = req.body.name
    const newCity = req.body.city
    const newColor = req.body.color
    const newAnswers = req.body.answers
    const newReputation = req.body.reputation
    names.push( id: newId, name: newName, city: newCity,
    color: newColor, answers: newAnswers, reputation: newReputation )
    res
    .status(201)
    .json( id: newId )
    )

    app.put('/names', (req, res) =>
    const id = req.body
    const indexToEdit = names.findIndex(obj => obj.id === id )
    names[indexToEdit].name = req.body.name
    names[indexToEdit].city = req.body.city
    names[indexToEdit].color = req.body.color
    names[indexToEdit].answers = req.body.answers
    names[indexToEdit].reputation = req.body.reputation
    const editName = names[indexToEdit].name
    const editCity = names[indexToEdit].city
    const editColor = names[indexToEdit].color
    const editAnswers = names[indexToEdit].answers
    const editReputation = names[indexToEdit].reputation
    res
    .status(200)
    .json( name: editName, city: editCity,
    color: editColor, answers: editAnswers, reputation: editReputation)
    )

    app.delete('/names', (req, res) =>
    const id = req.body
    const indexToRemove = names.findIndex(obj => obj.id === id )
    names.splice(indexToRemove, 1)
    res
    .status(200)
    .json( status: 'success')
    )

    app.listen(3000, () =>
    console.log('listening on 3000')
    )






    share|improve this question





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I build a RESTful CRUD API with Node.js and Express. This application allows, for example, to create a table on the client side with the ability to add, update and remove data by ID.
      I generate an array of random data for the availability of data when you start working on the client side with the table.
      I will be grateful for tips on improving the style.



      const express = require('express')
      const bodyParser = require('body-parser')
      const crypto = require("crypto");
      const cors = require('cors')

      const getId = () => crypto.randomBytes(16).toString("hex");
      const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
      const nameList = ['Andrew', 'John', 'Sebastian', 'Owen', 'Luke', 'Anthony', 'Dylan']
      const cityList = ['Riga', 'Berlin', 'Paris', 'Tokyo', 'London', 'Milan', 'Madrid']
      const colorList = ['red', 'blue', 'green']
      const getRandomStr = (str) => (str[getRandomInt(0, str.length)])

      const app = express()

      const names = [
      id: getId(),
      name: 'Li',
      city: 'Tokyo',
      color: 'blue',
      answers: '10',
      reputation: '1725'
      ]

      const generateNames = () =>
      for (let i=0; i<50; i++)
      const newId = getId()
      const newName = getRandomStr(nameList)
      const newCity = getRandomStr(cityList)
      const newColor = getRandomStr(colorList)
      const newAnswers = getRandomInt(10, 20).toString()
      const newReputation = getRandomInt(1500, 2000).toString()
      names.push( id: newId, name: newName, city: newCity,
      color: newColor, answers: newAnswers, reputation: newReputation )


      generateNames()

      app.use(bodyParser.json())

      app.use(cors())

      app.get('/names', (req, res) =>
      res.json(names)
      )

      app.post('/names', (req, res) =>
      const newId = getId()
      const newName = req.body.name
      const newCity = req.body.city
      const newColor = req.body.color
      const newAnswers = req.body.answers
      const newReputation = req.body.reputation
      names.push( id: newId, name: newName, city: newCity,
      color: newColor, answers: newAnswers, reputation: newReputation )
      res
      .status(201)
      .json( id: newId )
      )

      app.put('/names', (req, res) =>
      const id = req.body
      const indexToEdit = names.findIndex(obj => obj.id === id )
      names[indexToEdit].name = req.body.name
      names[indexToEdit].city = req.body.city
      names[indexToEdit].color = req.body.color
      names[indexToEdit].answers = req.body.answers
      names[indexToEdit].reputation = req.body.reputation
      const editName = names[indexToEdit].name
      const editCity = names[indexToEdit].city
      const editColor = names[indexToEdit].color
      const editAnswers = names[indexToEdit].answers
      const editReputation = names[indexToEdit].reputation
      res
      .status(200)
      .json( name: editName, city: editCity,
      color: editColor, answers: editAnswers, reputation: editReputation)
      )

      app.delete('/names', (req, res) =>
      const id = req.body
      const indexToRemove = names.findIndex(obj => obj.id === id )
      names.splice(indexToRemove, 1)
      res
      .status(200)
      .json( status: 'success')
      )

      app.listen(3000, () =>
      console.log('listening on 3000')
      )






      share|improve this question











      I build a RESTful CRUD API with Node.js and Express. This application allows, for example, to create a table on the client side with the ability to add, update and remove data by ID.
      I generate an array of random data for the availability of data when you start working on the client side with the table.
      I will be grateful for tips on improving the style.



      const express = require('express')
      const bodyParser = require('body-parser')
      const crypto = require("crypto");
      const cors = require('cors')

      const getId = () => crypto.randomBytes(16).toString("hex");
      const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
      const nameList = ['Andrew', 'John', 'Sebastian', 'Owen', 'Luke', 'Anthony', 'Dylan']
      const cityList = ['Riga', 'Berlin', 'Paris', 'Tokyo', 'London', 'Milan', 'Madrid']
      const colorList = ['red', 'blue', 'green']
      const getRandomStr = (str) => (str[getRandomInt(0, str.length)])

      const app = express()

      const names = [
      id: getId(),
      name: 'Li',
      city: 'Tokyo',
      color: 'blue',
      answers: '10',
      reputation: '1725'
      ]

      const generateNames = () =>
      for (let i=0; i<50; i++)
      const newId = getId()
      const newName = getRandomStr(nameList)
      const newCity = getRandomStr(cityList)
      const newColor = getRandomStr(colorList)
      const newAnswers = getRandomInt(10, 20).toString()
      const newReputation = getRandomInt(1500, 2000).toString()
      names.push( id: newId, name: newName, city: newCity,
      color: newColor, answers: newAnswers, reputation: newReputation )


      generateNames()

      app.use(bodyParser.json())

      app.use(cors())

      app.get('/names', (req, res) =>
      res.json(names)
      )

      app.post('/names', (req, res) =>
      const newId = getId()
      const newName = req.body.name
      const newCity = req.body.city
      const newColor = req.body.color
      const newAnswers = req.body.answers
      const newReputation = req.body.reputation
      names.push( id: newId, name: newName, city: newCity,
      color: newColor, answers: newAnswers, reputation: newReputation )
      res
      .status(201)
      .json( id: newId )
      )

      app.put('/names', (req, res) =>
      const id = req.body
      const indexToEdit = names.findIndex(obj => obj.id === id )
      names[indexToEdit].name = req.body.name
      names[indexToEdit].city = req.body.city
      names[indexToEdit].color = req.body.color
      names[indexToEdit].answers = req.body.answers
      names[indexToEdit].reputation = req.body.reputation
      const editName = names[indexToEdit].name
      const editCity = names[indexToEdit].city
      const editColor = names[indexToEdit].color
      const editAnswers = names[indexToEdit].answers
      const editReputation = names[indexToEdit].reputation
      res
      .status(200)
      .json( name: editName, city: editCity,
      color: editColor, answers: editAnswers, reputation: editReputation)
      )

      app.delete('/names', (req, res) =>
      const id = req.body
      const indexToRemove = names.findIndex(obj => obj.id === id )
      names.splice(indexToRemove, 1)
      res
      .status(200)
      .json( status: 'success')
      )

      app.listen(3000, () =>
      console.log('listening on 3000')
      )








      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 18 at 19:04









      Kate Herasimenak

      3016




      3016

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192400%2frestful-crud-api-with-node-js-and-express%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192400%2frestful-crud-api-with-node-js-and-express%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods