From 6ab90e17a4bcf6230dc898d05604cb586ed04d00 Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Mon, 6 Jan 2025 10:20:47 +0000 Subject: [PATCH 1/9] Remove body-parser --- nodejs/basic-example/talkjs-backend/package-lock.json | 1 - nodejs/basic-example/talkjs-backend/package.json | 2 -- nodejs/basic-example/talkjs-backend/server.js | 7 ++----- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/nodejs/basic-example/talkjs-backend/package-lock.json b/nodejs/basic-example/talkjs-backend/package-lock.json index b292cef9..33953d7b 100644 --- a/nodejs/basic-example/talkjs-backend/package-lock.json +++ b/nodejs/basic-example/talkjs-backend/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "body-parser": "^1.19.2", "cors": "^2.8.5", "express": "^4.17.3", "lowdb": "^3.0.0" diff --git a/nodejs/basic-example/talkjs-backend/package.json b/nodejs/basic-example/talkjs-backend/package.json index cfe33766..0ad7cdeb 100644 --- a/nodejs/basic-example/talkjs-backend/package.json +++ b/nodejs/basic-example/talkjs-backend/package.json @@ -1,7 +1,6 @@ { "name": "integrating-a-nodejs-app-with-talkjs", "version": "1.0.0", - "description": "", "type": "module", "main": "server.js", "scripts": { @@ -12,7 +11,6 @@ "author": "", "license": "ISC", "dependencies": { - "body-parser": "^1.19.2", "cors": "^2.8.5", "express": "^4.17.3", "lowdb": "^3.0.0" diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index e5ec5a78..8aa456f0 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -1,8 +1,5 @@ import express from "express"; - import cors from "cors"; -import bodyParser from "body-parser"; - import { LowSync, JSONFileSync } from "lowdb"; const adapter = new JSONFileSync("users.json"); @@ -14,8 +11,8 @@ const app = express(); const port = 3000; app.use(cors()); -app.use(bodyParser.urlencoded({ extended: false })); -app.use(bodyParser.json()); +app.use(express.urlencoded({ extended: false })); +app.use(express.json()); app.post("/createUser", (req, res) => { const id = req.body.id; From 14b64512fa78e62260fe339db97eb4518e7c9645 Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Mon, 6 Jan 2025 10:52:21 +0000 Subject: [PATCH 2/9] Remove unnecessary line --- nodejs/basic-example/talkjs-backend/server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index 8aa456f0..ce2b9bcb 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -11,7 +11,6 @@ const app = express(); const port = 3000; app.use(cors()); -app.use(express.urlencoded({ extended: false })); app.use(express.json()); app.post("/createUser", (req, res) => { From fb9c8d2d443fecd500ece447cdfd925c3eba2652 Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Mon, 6 Jan 2025 11:26:50 +0000 Subject: [PATCH 3/9] Update title --- nodejs/basic-example/talkjs-frontend/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodejs/basic-example/talkjs-frontend/index.html b/nodejs/basic-example/talkjs-frontend/index.html index 8b03ea81..915d408c 100644 --- a/nodejs/basic-example/talkjs-frontend/index.html +++ b/nodejs/basic-example/talkjs-frontend/index.html @@ -3,7 +3,7 @@ - TalkJS Node JS Integration + TalkJS with NodeJS example From d3f8044691cdc18bfd0f627fdf9885df30eca16c Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Mon, 6 Jan 2025 11:52:33 +0000 Subject: [PATCH 4/9] Script tag remove async defer --- nodejs/basic-example/talkjs-frontend/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodejs/basic-example/talkjs-frontend/index.html b/nodejs/basic-example/talkjs-frontend/index.html index 915d408c..823be729 100644 --- a/nodejs/basic-example/talkjs-frontend/index.html +++ b/nodejs/basic-example/talkjs-frontend/index.html @@ -9,7 +9,6 @@ - +
Loading chat... From 2f0e737ecada6790122fb17807dacc1da5872bdc Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Mon, 6 Jan 2025 11:52:49 +0000 Subject: [PATCH 5/9] Remove blank line --- nodejs/basic-example/talkjs-backend/server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index ce2b9bcb..63e4ebf4 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -10,7 +10,6 @@ db.data ||= { users: [] }; const app = express(); const port = 3000; app.use(cors()); - app.use(express.json()); app.post("/createUser", (req, res) => { From e5a8bad1503f3ea3efd3d5a9795de1a924fd54bf Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Tue, 7 Jan 2025 09:57:23 +0000 Subject: [PATCH 6/9] Clarify LowDB initialization --- nodejs/basic-example/talkjs-backend/server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index 63e4ebf4..2ed6ad63 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -5,6 +5,7 @@ import { LowSync, JSONFileSync } from "lowdb"; const adapter = new JSONFileSync("users.json"); const db = new LowSync(adapter); db.read(); +// Initialize with an empty array if users.json is empty or doesn't exist db.data ||= { users: [] }; const app = express(); From cdd9326ecda83cffe623da6bd3cf6c72e7088de9 Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Tue, 7 Jan 2025 13:37:27 +0000 Subject: [PATCH 7/9] Add welcomeMessage --- nodejs/basic-example/talkjs-backend/server.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nodejs/basic-example/talkjs-backend/server.js b/nodejs/basic-example/talkjs-backend/server.js index 2ed6ad63..9f22f655 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -19,12 +19,14 @@ app.post("/createUser", (req, res) => { const email = req.body.email; const photoUrl = req.body.photoUrl; const role = req.body.role; + const welcomeMessage = req.body.welcomeMessage; db.data.users.push({ id: id, name: name, email: email, photoUrl: photoUrl, role: role, + welcomeMessage: welcomeMessage, }); db.write(); res.status(200).send("User created successfully"); From 722777cac40a9fd4bfc64e6435daa4cc6a9b68be Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Tue, 7 Jan 2025 13:43:34 +0000 Subject: [PATCH 8/9] Make ids obviously strings --- nodejs/basic-example/talkjs-backend/users.json | 4 ++-- nodejs/basic-example/talkjs-frontend/script.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nodejs/basic-example/talkjs-backend/users.json b/nodejs/basic-example/talkjs-backend/users.json index 44d3ecc8..8f0e8fcd 100644 --- a/nodejs/basic-example/talkjs-backend/users.json +++ b/nodejs/basic-example/talkjs-backend/users.json @@ -1,7 +1,7 @@ { "users": [ { - "id": "1", + "id": "alice", "name": "Alice", "email": "alice@example.com", "photoUrl": "https://talkjs.com/new-web/avatar-7.jpg", @@ -9,7 +9,7 @@ "welcomeMessage": "Hi 👋" }, { - "id": "2", + "id": "sebastian", "name": "Sebastian", "email": "sebastian@example.com", "photoUrl": "https://talkjs.com/new-web/avatar-2.jpg", diff --git a/nodejs/basic-example/talkjs-frontend/script.js b/nodejs/basic-example/talkjs-frontend/script.js index e757e97b..44765ec6 100644 --- a/nodejs/basic-example/talkjs-frontend/script.js +++ b/nodejs/basic-example/talkjs-frontend/script.js @@ -13,8 +13,8 @@ const getUser = async (id) => { (async function () { await Talk.ready; - const alice = await getUser(1); - const sebastian = await getUser(2); + const alice = await getUser("alice"); + const sebastian = await getUser("sebastian"); const session = new Talk.Session({ appId: "", // replace with your app ID me: sebastian, From 4f123759067066987c00c3a16c178fdb3a8aec8e Mon Sep 17 00:00:00 2001 From: Lucy Keer Date: Wed, 8 Jan 2025 14:52:27 +0000 Subject: [PATCH 9/9] Add missing welcomeMessage to frontend --- nodejs/basic-example/talkjs-frontend/script.js | 1 + 1 file changed, 1 insertion(+) diff --git a/nodejs/basic-example/talkjs-frontend/script.js b/nodejs/basic-example/talkjs-frontend/script.js index 44765ec6..9fead64a 100644 --- a/nodejs/basic-example/talkjs-frontend/script.js +++ b/nodejs/basic-example/talkjs-frontend/script.js @@ -7,6 +7,7 @@ const getUser = async (id) => { photoUrl: data.photoUrl, email: data.email, role: data.role, + welcomeMessage: data.welcomeMessage, }); return user; };