From 82985015933627e547438360cda205445e86f67e Mon Sep 17 00:00:00 2001 From: Jeff See Date: Mon, 18 Aug 2025 14:15:09 -0700 Subject: [PATCH 1/5] Updates express to esm --- solutions/express/api/index.ts | 55 -------- solutions/express/package.json | 10 +- .../express/public/{ => images}/logo.png | Bin solutions/express/src/index.ts | 121 ++++++++++++++++++ solutions/express/tsconfig.json | 12 ++ 5 files changed, 141 insertions(+), 57 deletions(-) delete mode 100644 solutions/express/api/index.ts rename solutions/express/public/{ => images}/logo.png (100%) create mode 100644 solutions/express/src/index.ts create mode 100644 solutions/express/tsconfig.json diff --git a/solutions/express/api/index.ts b/solutions/express/api/index.ts deleted file mode 100644 index 9f725d38f7..0000000000 --- a/solutions/express/api/index.ts +++ /dev/null @@ -1,55 +0,0 @@ -// api/index.js -const express = require('express'); -const path = require('path'); - -const app = express(); - -// Serve static files from /public -app.use(express.static(path.join(__dirname, '..', 'public'))); - -// Home route - HTML -app.get('/', (req, res) => { - res.type('html').send(` - - - - - Express on Vercel - - - - -

Welcome to Express on Vercel 🚀

-

This is a minimal example without a database or forms.

- Logo - - - `); -}); - -app.get('/about', function (req, res) { - res.sendFile(path.join(__dirname, '..', 'components', 'about.htm')); -}); - -// Example API endpoint - JSON -app.get('/api-data', (req, res) => { - res.json({ - message: 'Here is some sample API data', - items: ['apple', 'banana', 'cherry'] - }); -}); - -// Health check -app.get('/healthz', (req, res) => { - res.status(200).json({ status: 'ok', timestamp: new Date().toISOString() }); -}); - -// Local dev listener (ignored on Vercel) -app.listen(3000, () => console.log('Server running on http://localhost:3000')); - -module.exports = app; diff --git a/solutions/express/package.json b/solutions/express/package.json index 59605b87af..ec4822f90b 100644 --- a/solutions/express/package.json +++ b/solutions/express/package.json @@ -2,7 +2,7 @@ "name": "express", "version": "1.0.0", "description": "", - "main": "index.ts", + "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node api/index.ts" @@ -11,6 +11,12 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.18.2" + "@types/express": "^5.0.0", + "@vercel/postgres": "0.10.0", + "dotenv": "^16.4.0", + "express": "5.1.0" + }, + "devDependencies": { + "@types/node": "^22.0.0" } } diff --git a/solutions/express/public/logo.png b/solutions/express/public/images/logo.png similarity index 100% rename from solutions/express/public/logo.png rename to solutions/express/public/images/logo.png diff --git a/solutions/express/src/index.ts b/solutions/express/src/index.ts new file mode 100644 index 0000000000..a05e07f9f0 --- /dev/null +++ b/solutions/express/src/index.ts @@ -0,0 +1,121 @@ +import dotenv from 'dotenv'; +import express, { type Request, type Response } from 'express'; +import { sql } from '@vercel/postgres'; +import bodyParser from 'body-parser'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +dotenv.config(); + +const app = express(); + +const __filename = fileURLToPath(import.meta.url); +const rootDir = path.dirname(__filename); + + +// Serve static files from the public directory +app.use(express.static(path.join(rootDir, '..', 'public'))); + +app.get('/', function (req: Request, res: Response) { + res.sendFile(path.join(rootDir, '..', 'components', 'home.htm')); +}); + +app.get('/about', function (req: Request, res: Response) { + res.sendFile(path.join(rootDir, '..', 'components', 'about.htm')); +}); + +app.get('/uploadUser', function (req: Request, res: Response) { + res.sendFile(path.join(rootDir, '..', 'components', 'user_upload_form.htm')); +}); + +type UploadBody = { user_id: string; name: string; email: string }; + +app.post('/uploadSuccessful', bodyParser.urlencoded({ extended: false }), async ( + req: Request, any, UploadBody>, + res: Response +) => { + try { + await sql`INSERT INTO Users (Id, Name, Email) VALUES (${req.body.user_id}, ${req.body.name}, ${req.body.email});`; + res.status(200).send('

User added successfully

'); + } catch (error) { + console.error(error); + res.status(500).send('Error adding user'); + } +}); + +app.get('/allUsers', async (req: Request, res: Response) => { + try { + const users = await sql`SELECT * FROM Users;`; + if (users && users.rows.length > 0) { + let tableContent = users.rows + .map( + (user: any) => + ` + ${user.id} + ${user.name} + ${user.email} + ` + ) + .join(''); + + res.status(200).send(` + + + Users + + + +

Users

+ + + + + + + + + + ${tableContent} + +
User IDNameEmail
+
+ Home + Add User +
+ + + `); + } else { + res.status(404).send('Users not found'); + } + } catch (error) { + console.error(error); + res.status(500).send('Error retrieving users'); + } +}); + +app.listen(3009, () => console.log('Server ready on port 3000.')); + +export default app; diff --git a/solutions/express/tsconfig.json b/solutions/express/tsconfig.json new file mode 100644 index 0000000000..4fc0685295 --- /dev/null +++ b/solutions/express/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "outDir": "./dist", + "rootDir": ".", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true + }, +} From 784abfa4334fae887dc6471df4aa5906279decba Mon Sep 17 00:00:00 2001 From: Jeff See Date: Mon, 18 Aug 2025 14:40:10 -0700 Subject: [PATCH 2/5] use newer code --- solutions/express/src/index.ts | 130 +++++++++------------------------ 1 file changed, 35 insertions(+), 95 deletions(-) diff --git a/solutions/express/src/index.ts b/solutions/express/src/index.ts index a05e07f9f0..babc73dc9c 100644 --- a/solutions/express/src/index.ts +++ b/solutions/express/src/index.ts @@ -1,7 +1,5 @@ import dotenv from 'dotenv'; import express, { type Request, type Response } from 'express'; -import { sql } from '@vercel/postgres'; -import bodyParser from 'body-parser'; import path from 'path'; import { fileURLToPath } from 'url'; @@ -12,110 +10,52 @@ const app = express(); const __filename = fileURLToPath(import.meta.url); const rootDir = path.dirname(__filename); - // Serve static files from the public directory app.use(express.static(path.join(rootDir, '..', 'public'))); -app.get('/', function (req: Request, res: Response) { - res.sendFile(path.join(rootDir, '..', 'components', 'home.htm')); +// Home route - HTML +app.get('/', (req, res) => { + res.type('html').send(` + + + + + Express on Vercel + + + + +

Welcome to Express on Vercel 🚀

+

This is a minimal example without a database or forms.

+ Logo + + + `); }); -app.get('/about', function (req: Request, res: Response) { - res.sendFile(path.join(rootDir, '..', 'components', 'about.htm')); -}); -app.get('/uploadUser', function (req: Request, res: Response) { - res.sendFile(path.join(rootDir, '..', 'components', 'user_upload_form.htm')); +app.get('/about', function (req: Request, res: Response) { + res.sendFile(path.join(rootDir, '..', 'components', 'about.htm')); }); -type UploadBody = { user_id: string; name: string; email: string }; - -app.post('/uploadSuccessful', bodyParser.urlencoded({ extended: false }), async ( - req: Request, any, UploadBody>, - res: Response -) => { - try { - await sql`INSERT INTO Users (Id, Name, Email) VALUES (${req.body.user_id}, ${req.body.name}, ${req.body.email});`; - res.status(200).send('

User added successfully

'); - } catch (error) { - console.error(error); - res.status(500).send('Error adding user'); - } +// Example API endpoint - JSON +app.get('/api-data', (req, res) => { + res.json({ + message: 'Here is some sample API data', + items: ['apple', 'banana', 'cherry'] + }); }); -app.get('/allUsers', async (req: Request, res: Response) => { - try { - const users = await sql`SELECT * FROM Users;`; - if (users && users.rows.length > 0) { - let tableContent = users.rows - .map( - (user: any) => - ` - ${user.id} - ${user.name} - ${user.email} - ` - ) - .join(''); - - res.status(200).send(` - - - Users - - - -

Users

- - - - - - - - - - ${tableContent} - -
User IDNameEmail
-
- Home - Add User -
- - - `); - } else { - res.status(404).send('Users not found'); - } - } catch (error) { - console.error(error); - res.status(500).send('Error retrieving users'); - } +// Health check +app.get('/healthz', (req, res) => { + res.status(200).json({ status: 'ok', timestamp: new Date().toISOString() }); }); -app.listen(3009, () => console.log('Server ready on port 3000.')); +app.listen(3000, () => console.log('Server ready on port 3000.')); export default app; From 4d9dcacad80cc5ff51974c0e09275845783ccd15 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Mon, 18 Aug 2025 14:41:36 -0700 Subject: [PATCH 3/5] Remove scripts --- solutions/express/package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/solutions/express/package.json b/solutions/express/package.json index ec4822f90b..3e48e5e3b7 100644 --- a/solutions/express/package.json +++ b/solutions/express/package.json @@ -3,10 +3,6 @@ "version": "1.0.0", "description": "", "type": "module", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node api/index.ts" - }, "keywords": [], "author": "", "license": "ISC", From 46893404a0b602951e71ab8a4ed4064b6aae9c23 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Mon, 18 Aug 2025 14:43:26 -0700 Subject: [PATCH 4/5] remove vercel.json --- solutions/express/vercel.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 solutions/express/vercel.json diff --git a/solutions/express/vercel.json b/solutions/express/vercel.json deleted file mode 100644 index ec61493355..0000000000 --- a/solutions/express/vercel.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "rewrites": [{ "source": "/(.*)", "destination": "/api" }] -} From 9ad01c0323a6b0453042c72864a9cc79b32b33ea Mon Sep 17 00:00:00 2001 From: Jeff See Date: Thu, 21 Aug 2025 12:09:22 -0700 Subject: [PATCH 5/5] Update index.ts --- solutions/express/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/solutions/express/src/index.ts b/solutions/express/src/index.ts index babc73dc9c..36e6d8c1fb 100644 --- a/solutions/express/src/index.ts +++ b/solutions/express/src/index.ts @@ -56,6 +56,4 @@ app.get('/healthz', (req, res) => { res.status(200).json({ status: 'ok', timestamp: new Date().toISOString() }); }); -app.listen(3000, () => console.log('Server ready on port 3000.')); - export default app;