diff --git a/node-page/app.js b/node-page/app.js index 9cf6290..b04eb84 100644 --- a/node-page/app.js +++ b/node-page/app.js @@ -1,23 +1,27 @@ -const express = require('express'); -const fs = require('fs'); -const path = require('path'); +import express from 'express'; +import { readFile } from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; const app = express(); 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 app.use(express.static(path.join(__dirname, 'public'))); // Endpoint to get the list of users -app.get('/api/users', (req, res) => { - fs.readFile(path.join(__dirname, 'users.json'), 'utf8', (err, data) => { - if (err) { - res.status(500).send('Error reading user data'); - return; - } +app.get('/api/users', async (req, res) => { + try { + const data = await readFile(path.join(__dirname, 'users.json'), 'utf8'); const users = JSON.parse(data); res.json(users); - }); + } catch (err) { + res.status(500).send('Error reading user data'); + } }); // Start the server diff --git a/node-page/package.json b/node-page/package.json index f184b9c..a9aa717 100644 --- a/node-page/package.json +++ b/node-page/package.json @@ -2,7 +2,8 @@ "name": "page", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "app.js", + "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node app.js"