Compare commits
2 Commits
79ca39d639
...
405a465a18
| Author | SHA1 | Date | |
|---|---|---|---|
| 405a465a18 | |||
| 8b7ed5f523 |
|
|
@ -1,23 +1,27 @@
|
||||||
const express = require('express');
|
import express from 'express';
|
||||||
const fs = require('fs');
|
import { readFile } from 'fs/promises';
|
||||||
const path = require('path');
|
import path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 3000;
|
const PORT = 3000;
|
||||||
|
|
||||||
|
// Helper variables for __dirname equivalent in ES modules
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
// Serve static files from the "public" directory
|
// Serve static files from the "public" directory
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
// Endpoint to get the list of users
|
// Endpoint to get the list of users
|
||||||
app.get('/api/users', (req, res) => {
|
app.get('/api/users', async (req, res) => {
|
||||||
fs.readFile(path.join(__dirname, 'users.json'), 'utf8', (err, data) => {
|
try {
|
||||||
if (err) {
|
const data = await readFile(path.join(__dirname, 'users.json'), 'utf8');
|
||||||
res.status(500).send('Error reading user data');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const users = JSON.parse(data);
|
const users = JSON.parse(data);
|
||||||
res.json(users);
|
res.json(users);
|
||||||
});
|
} catch (err) {
|
||||||
|
res.status(500).send('Error reading user data');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
"name": "page",
|
"name": "page",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "app.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"start": "node app.js"
|
"start": "node app.js"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user