Restful CRUD API with Node.js and Express

Clash 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')
)
javascript node.js rest express.js crud
add a comment |Â
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')
)
javascript node.js rest express.js crud
add a comment |Â
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')
)
javascript node.js rest express.js crud
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')
)
javascript node.js rest express.js crud
asked Apr 18 at 19:04
Kate Herasimenak
3016
3016
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password