{"size": 969, "ext": "js", "max_stars_repo_path": "2-expressJs-tutorial/7-post-request-javascript.js", "max_stars_repo_name": "brajith/NodeJs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "2-expressJs-tutorial/7-post-request-javascript.js", "max_issues_repo_name": "brajith/NodeJs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "2-expressJs-tutorial/7-post-request-javascript.js", "max_forks_repo_name": "brajith/NodeJs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5348837209, "max_line_length": 89, "alphanum_fraction": 0.5861713106}
const express = require('express')
const { people } = require('./Data/data')
const app = express()

app.use(express.static('./methods-public'))

//This middleware helps to get the Json data from the POST request
app.use(express.json())

app.get('/api/people', (req, res) => {
    res.send({
        success: true,
        data: people
    })
})

app.post('/login', (req, res) => {
    const { name } = req.body

    if(name) {
        res.status(200).send(`welcome ${ name }`)
    } 
    else {
        res.status(401).send(`Please enter the credentials`)
    }
})

//Post data sent using javascript 
app.post('/api/people', (req, res) => {
    const { name } = req.body

    if(!name) {
        return res.status(400).json({ success: false, msg: "please provide name value" })
    }
    people.push({ id: 6, name: name})
    res.status(201).json({ success: true, person: name })
})


app.listen(5000, () => {
    console.log("Server is listining at port 5000...");
})
