Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bbachi
Created April 24, 2021 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbachi/2066119acd68b98f2876241c525d064a to your computer and use it in GitHub Desktop.
Save bbachi/2066119acd68b98f2876241c525d064a to your computer and use it in GitHub Desktop.
NodeJS
const express = require('express');
const path = require('path');
const app = express(),
bodyParser = require("body-parser");
port = 8080;
// place holder for the data
const users = [
{
firstName: "first1",
lastName: "last1",
email: "abc@gmail.com"
},
{
firstName: "first2",
lastName: "last2",
email: "abc@gmail.com"
},
{
firstName: "first3",
lastName: "last3",
email: "abc@gmail.com"
}
];
app.use(bodyParser.json());
app.use(express.static(process.cwd()+"/my-app/build/"));
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;
console.log('Adding user:::::', user);
users.push(user);
res.json("user addedd");
});
app.get('/', (req,res) => {
res.sendFile(process.cwd()+"/my-app/build/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