-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathserver.js
30 lines (26 loc) · 926 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const express = require('express');
const fs = require('fs');
const path = require('path');
var cors = require('cors')
const app = express();
app.use(cors());
app.get("/", (req, res) => {
var response = [];
const allData = fs.readdirSync("./public/data").filter(file => path.extname(file) === ".json");
console.log(allData);
allData.map((person) => {
const fileData = fs.readFileSync(path.join('./public/data', person));
const personData = JSON.parse(fileData.toString());
response.push(personData);
});
res.json(response);
});
app.get("/:username", (req, res) => {
const filename = req.params.username + ".json";
const data = fs.readFileSync(path.join('./public/data', filename));
const parsedData = JSON.parse(data.toString());
res.json(parsedData);
});
app.listen(process.env.PORT || 3001, () => {
console.log('express server listening on 3001');
});