#How do i represent my json data stored locally on front end

1 messages · Page 1 of 1 (latest)

rustic pelican
#

Here are my code
i want to represent the my data on frontend by hitting a specific API using get request it should respone with json objects

here is my code :
server.ts

import express from 'express';
const app = express();

const PORT = 3000;

interface Account {
    id: number;
    name: string;
    accType: string;
    email: string;
    Data_Acc_created: Date;
    balance: number;
    Nooftransaction: number;
  }
let data: Account[] =  [{id: 1, name: "example", accType: "Saving",email:"[email protected]", Data_Acc_created: new Date("2015-01-16"), balance: 100, Nooftransaction: 0},
{id: 2, name: "test", accType: "Current",email:"[email protected]", Data_Acc_created: new Date("2016-01-16"), balance: 200, Nooftransaction: 2}]

app.get('/account/:id',(req:any,res:any)=>{
    const id = parseInt(req.params.id);
    const getAccount = data.find(element => element.id === id);
    if(!getAccount){
        res.status(500).send("account not found");
    }else{
        res.json(getAccount);
    }
});

app.post('/createnewaccount', (req:any, res:any)=>{
  const {name, accType, email} = req.body;

  const newAccount = {
    id : data.length + 1,
    name,
    accType,
    email,
    Data_Acc_created : new Date(),
    balance : 0,
    Nooftransaction : 0
  }
  
  data.push(newAccount);

})



app.listen(3000, () =>
  console.log('Example app listening on port 3000!'),
);

module.exports = app;

here is my index.html

<!DOCTYPE html>
<html>
<head>
  <title>Account Details</title>
</head>
<body>

    <script src = "server.js"></script>
</body>
</html>

im not front end guy but any procedure to do this would be helpful

ocean coral
#

you havent said anything about what the issue actually is that you want help with, just what code you have.

#

also why are you loading your server code into a html file that will be presented to the client?

#

the server.ts needs to be run on a server with node or something similar, completely separate from what's running in the browser

#

and then the script in your html needs to use fetch to make a request to that server that is running