Skip to content

Instantly share code, notes, and snippets.

@niinpatel
Created June 14, 2018 07:11
Show Gist options
  • Save niinpatel/918c92f0b4ecaa366fa5a0a92ff84188 to your computer and use it in GitHub Desktop.
Save niinpatel/918c92f0b4ecaa366fa5a0a92ff84188 to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
let applications = [];
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.post('/apply', (req, res) => {
let name = req.body.name;
let email = req.body.email;
let phone = req.body.phone;
let about = req.body.about;
let application = {id: Math.random().toString(36).substring(2), name: name, email: email, phone: phone, about: about};
applications.push(application);
res.send('Your application has been sent')
});
app.get('/all_applications', (req, res) => {
res.send(applications)
});
app.put('/update/:id', (req, res) => {
let applicant = applications.find(applicant => applicant.id === req.params.id);
let name = req.body.name || applicant.name;
let email = req.body.email || applicant.email;
let phone = req.body.phone || applicant.phone;
let about = req.body.about || applicant.about;
let id = applicant.id;
applications.map(applicant => {
if(applicant.id === id){
applicant.name = name;
applicant.email = email;
applicant.phone = phone;
applicant.about = about;
return applicant
}
});
res.send('Your application has been updated')
});
app.delete('/delete/:id', (req, res) => {
applications = applications.filter(applicant => applicant.id !== req.params.id);
res.send('Your application has been removed')
});
app.listen(3000, () => console.log('listening on post 3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment