Created
April 23, 2021 02:28
-
-
Save bbachi/13a9505338886d39cbddaceb4b3bef7c to your computer and use it in GitHub Desktop.
Github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const randomId = require('random-id'); | |
const app = express(), | |
bodyParser = require("body-parser"); | |
port = 8080; | |
// place holder for the data | |
const users = [ | |
{ | |
id: "1", | |
firstName: "first1", | |
lastName: "last1", | |
email: "abc@gmail.com" | |
}, | |
{ | |
id: "2", | |
firstName: "first2", | |
lastName: "last2", | |
email: "abc@gmail.com" | |
}, | |
{ | |
id: "3", | |
firstName: "first3", | |
lastName: "last3", | |
email: "abc@gmail.com" | |
} | |
]; | |
app.use(bodyParser.json()); | |
app.use(express.static(process.cwd() + '/my-app/dist')); | |
app.get('/api/users', (req, res) => { | |
console.log('api/users called!!!!!!!') | |
res.json(users); | |
}); | |
app.post('/api/user', (req, res) => { | |
const user = req.body.user; | |
user.id = randomId(10); | |
console.log('Adding user:::::', user); | |
users.push(user); | |
res.json("user addedd"); | |
}); | |
app.get('/', (req,res) => { | |
res.sendFile(process.cwd() + '/my-app/dist/index.html'); | |
}); | |
app.listen(port, () => { | |
console.log(`Server listening on the port::${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment