Skip to content

Commit 9454d64

Browse files
committed
Basec NodeJS support
1 parent e0c28d1 commit 9454d64

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

server-nodejs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

server-nodejs/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# Coming soon!
1+
## NodeJS Server Setup
2+
3+
```
4+
npm init
5+
npm install express axios cors
6+
npm install --save-dev nodemon
7+
# Add: "scripts": { "start": "nodemon index.js" },
8+
echo node_modules/ > .gitignore
9+
touch index.js
10+
# Add the code
11+
npm run start
12+
```
13+
14+
Edit the package.json file

server-nodejs/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const axios = require("axios");
4+
5+
const app = express();
6+
app.use(express.json());
7+
app.use(cors({ origin: true }));
8+
9+
const CHAT_ENGINE_PROJECT_ID = "";
10+
const CHAT_ENGINE_PRIVATE_KEY = "";
11+
12+
app.post("/signup", async (req, res) => {
13+
const { username, secret, email, first_name, last_name } = req.body;
14+
15+
// Store a user-copy on Chat Engine!
16+
// Docs at rest.chatengine.io
17+
try {
18+
const r = await axios.post(
19+
"https://api.chatengine.io/users/",
20+
{ username, secret, email, first_name, last_name },
21+
{ headers: { "Private-Key": CHAT_ENGINE_PRIVATE_KEY } }
22+
);
23+
return res.status(r.status).json(r.data);
24+
} catch (e) {
25+
return res.status(e.response.status).json(e.response.data);
26+
}
27+
});
28+
29+
app.post("/login", async (req, res) => {
30+
const { username, secret } = req.body;
31+
32+
// Fetch this user from Chat Engine in this project!
33+
// Docs at rest.chatengine.io
34+
try {
35+
const r = await axios.get("https://api.chatengine.io/users/me/", {
36+
headers: {
37+
"Project-ID": CHAT_ENGINE_PROJECT_ID,
38+
"User-Name": username,
39+
"User-Secret": secret,
40+
},
41+
});
42+
return res.status(r.status).json(r.data);
43+
} catch (e) {
44+
return res.status(e.response.status).json(e.response.data);
45+
}
46+
});
47+
48+
// vvv On port 3001!
49+
app.listen(3001);

server-nodejs/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "server-node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "nodemon index.js"
8+
},
9+
"author": "Adam La Morre",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"nodemon": "^2.0.20"
13+
},
14+
"dependencies": {
15+
"axios": "^1.2.0",
16+
"cors": "^2.8.5",
17+
"express": "^4.18.2"
18+
}
19+
}

0 commit comments

Comments
 (0)