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..9f22f655 100644 --- a/nodejs/basic-example/talkjs-backend/server.js +++ b/nodejs/basic-example/talkjs-backend/server.js @@ -1,21 +1,17 @@ import express from "express"; - import cors from "cors"; -import bodyParser from "body-parser"; - 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(); const port = 3000; app.use(cors()); - -app.use(bodyParser.urlencoded({ extended: false })); -app.use(bodyParser.json()); +app.use(express.json()); app.post("/createUser", (req, res) => { const id = req.body.id; @@ -23,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"); 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/index.html b/nodejs/basic-example/talkjs-frontend/index.html index 8b03ea81..823be729 100644 --- a/nodejs/basic-example/talkjs-frontend/index.html +++ b/nodejs/basic-example/talkjs-frontend/index.html @@ -3,13 +3,12 @@ - TalkJS Node JS Integration + TalkJS with NodeJS example - +
Loading chat... diff --git a/nodejs/basic-example/talkjs-frontend/script.js b/nodejs/basic-example/talkjs-frontend/script.js index e757e97b..9fead64a 100644 --- a/nodejs/basic-example/talkjs-frontend/script.js +++ b/nodejs/basic-example/talkjs-frontend/script.js @@ -7,14 +7,15 @@ const getUser = async (id) => { photoUrl: data.photoUrl, email: data.email, role: data.role, + welcomeMessage: data.welcomeMessage, }); return user; }; (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,