Skip to content

Commit e4a2473

Browse files
author
Pankaj Patel
committed
twitter clone with basic functionality
1 parent 958048f commit e4a2473

16 files changed

+1098
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
*.spf

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
# express-mysql
1+
# express-mysql
2+
3+
4+
![](//res.cloudinary.com/time2hack/image/upload/q_auto:good/creating-rest-api-in-node-js-with-express-and-mysql-social-md.jpg)
5+
6+
Code for understanding for creating REST API in Node.js with Express and MySQL.
7+
8+
The article/tutorial can be be read here: https://time2hack.com/2019/09/creating-rest-api-in-node-js-with-express-and-mysql/
9+
10+
You cna run this collection in Postman with following button:
11+
<center>
12+
[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/addfc209f1ab0fc6c873)
13+
</center>
14+
15+
----
16+
17+
<center>
18+
![](./twitter-clone-postman.png)
19+
</center>
20+
21+
Or Download and import in Postman from here: <a href="./Twitter Clone.postman_collection.json" download>Download</a>

Twitter Clone.postman_collection.json

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
{
2+
"info": {
3+
"_postman_id": "78149020-40a3-4657-80a9-7683bdae2c78",
4+
"name": "Twitter Clone",
5+
"description": "This collection is created to help understand the Rest API creation on Node.js with Express and MySQL\n\nThe original article can be found here: https://time2hack.com/2019/09/creating-rest-api-in-node-js-with-express-and-mysql",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"name": "Tweets",
11+
"item": [
12+
{
13+
"name": "Feed",
14+
"request": {
15+
"method": "GET",
16+
"header": [],
17+
"url": {
18+
"raw": "{{url}}/tweets/feed",
19+
"host": [
20+
"{{url}}"
21+
],
22+
"path": [
23+
"tweets",
24+
"feed"
25+
]
26+
},
27+
"description": "User's Feed"
28+
},
29+
"response": []
30+
},
31+
{
32+
"name": "Tweet from Feed",
33+
"request": {
34+
"method": "GET",
35+
"header": [],
36+
"url": {
37+
"raw": "{{url}}/tweets/10",
38+
"host": [
39+
"{{url}}"
40+
],
41+
"path": [
42+
"tweets",
43+
"10"
44+
]
45+
},
46+
"description": "Tweet from User's Feed"
47+
},
48+
"response": []
49+
}
50+
],
51+
"protocolProfileBehavior": {}
52+
},
53+
{
54+
"name": "User",
55+
"item": [
56+
{
57+
"name": "Logged in User Profile",
58+
"request": {
59+
"method": "GET",
60+
"header": [],
61+
"url": {
62+
"raw": "{{url}}/user",
63+
"host": [
64+
"{{url}}"
65+
],
66+
"path": [
67+
"user"
68+
]
69+
}
70+
},
71+
"response": []
72+
},
73+
{
74+
"name": "Get User's Followers",
75+
"request": {
76+
"method": "GET",
77+
"header": [],
78+
"url": {
79+
"raw": "{{url}}/user/followers",
80+
"host": [
81+
"{{url}}"
82+
],
83+
"path": [
84+
"user",
85+
"followers"
86+
]
87+
}
88+
},
89+
"response": []
90+
},
91+
{
92+
"name": "Get Whom User is Following",
93+
"request": {
94+
"method": "GET",
95+
"header": [],
96+
"url": {
97+
"raw": "{{url}}/user/following",
98+
"host": [
99+
"{{url}}"
100+
],
101+
"path": [
102+
"user",
103+
"following"
104+
]
105+
}
106+
},
107+
"response": []
108+
},
109+
{
110+
"name": "User Follow new Friend",
111+
"request": {
112+
"method": "POST",
113+
"header": [],
114+
"body": {
115+
"mode": "formdata",
116+
"formdata": [
117+
{
118+
"key": "id",
119+
"value": "5",
120+
"type": "text"
121+
}
122+
]
123+
},
124+
"url": {
125+
"raw": "{{url}}/user/following",
126+
"host": [
127+
"{{url}}"
128+
],
129+
"path": [
130+
"user",
131+
"following"
132+
]
133+
}
134+
},
135+
"response": []
136+
},
137+
{
138+
"name": "Get Settings",
139+
"request": {
140+
"method": "GET",
141+
"header": [],
142+
"url": {
143+
"raw": "{{url}}/user/settings",
144+
"host": [
145+
"{{url}}"
146+
],
147+
"path": [
148+
"user",
149+
"settings"
150+
]
151+
}
152+
},
153+
"response": []
154+
},
155+
{
156+
"name": "Update Settings",
157+
"request": {
158+
"method": "PUT",
159+
"header": [],
160+
"body": {
161+
"mode": "formdata",
162+
"formdata": [
163+
{
164+
"key": "email",
165+
"value": "true",
166+
"type": "text"
167+
},
168+
{
169+
"key": "sms",
170+
"value": "false",
171+
"type": "text"
172+
}
173+
]
174+
},
175+
"url": {
176+
"raw": "{{url}}/user/settings",
177+
"host": [
178+
"{{url}}"
179+
],
180+
"path": [
181+
"user",
182+
"settings"
183+
]
184+
}
185+
},
186+
"response": []
187+
}
188+
],
189+
"protocolProfileBehavior": {}
190+
},
191+
{
192+
"name": "Register",
193+
"event": [
194+
{
195+
"listen": "test",
196+
"script": {
197+
"id": "ed766847-6bde-48fa-9675-d2b1631814dc",
198+
"exec": [
199+
"",
200+
"pm.test(\"Resposne has ID\", function () {",
201+
" var jsonData = pm.response.json();",
202+
" pm.expect(jsonData.id).to.not.eql(null);",
203+
"});",
204+
"pm.environment.set(\"user\", pm.response.json().id);",
205+
""
206+
],
207+
"type": "text/javascript"
208+
}
209+
}
210+
],
211+
"request": {
212+
"method": "POST",
213+
"header": [
214+
{
215+
"key": "Content-Type",
216+
"name": "Content-Type",
217+
"value": "application/x-www-form-urlencoded",
218+
"type": "text"
219+
}
220+
],
221+
"body": {
222+
"mode": "urlencoded",
223+
"urlencoded": [
224+
{
225+
"key": "username",
226+
"value": "jim",
227+
"type": "text"
228+
},
229+
{
230+
"key": "password",
231+
"value": "jim",
232+
"type": "text"
233+
}
234+
]
235+
},
236+
"url": {
237+
"raw": "{{url}}/auth/register",
238+
"host": [
239+
"{{url}}"
240+
],
241+
"path": [
242+
"auth",
243+
"register"
244+
],
245+
"query": [
246+
{
247+
"key": "username",
248+
"value": "",
249+
"disabled": true
250+
},
251+
{
252+
"key": "password",
253+
"value": "",
254+
"disabled": true
255+
}
256+
]
257+
},
258+
"description": "# Register New User\n---\n\n## Params\n\n`username`: `String`\n`password`: `String`\n\nReturns the new User's ID and Set's to Environment"
259+
},
260+
"response": []
261+
},
262+
{
263+
"name": "Login",
264+
"event": [
265+
{
266+
"listen": "test",
267+
"script": {
268+
"id": "6297fe46-d7b5-49f6-982f-5b0e0f467cc3",
269+
"exec": [
270+
"",
271+
"pm.test(\"Resposne has ID\", function () {",
272+
" var jsonData = pm.response.json();",
273+
" pm.expect(jsonData.id).to.not.eql(null);",
274+
"});",
275+
"pm.environment.set(\"user\", pm.response.json().id);",
276+
""
277+
],
278+
"type": "text/javascript"
279+
}
280+
}
281+
],
282+
"request": {
283+
"method": "POST",
284+
"header": [
285+
{
286+
"key": "Content-Type",
287+
"name": "Content-Type",
288+
"value": "application/x-www-form-urlencoded",
289+
"type": "text"
290+
}
291+
],
292+
"body": {
293+
"mode": "urlencoded",
294+
"urlencoded": [
295+
{
296+
"key": "username",
297+
"value": "pankaj",
298+
"type": "text"
299+
},
300+
{
301+
"key": "password",
302+
"value": "pankaj",
303+
"type": "text"
304+
}
305+
]
306+
},
307+
"url": {
308+
"raw": "{{url}}/auth/login",
309+
"host": [
310+
"{{url}}"
311+
],
312+
"path": [
313+
"auth",
314+
"login"
315+
]
316+
},
317+
"description": "# Login\n---\n\n## Params\n\n`username`: `String`\n`password`: `String`\n\nReturns the User's ID and Set's to Environment"
318+
},
319+
"response": []
320+
}
321+
],
322+
"variable": [
323+
{
324+
"id": "363978da-cd5b-42e5-97e7-a8f9aab3fbd0",
325+
"key": "user",
326+
"value": "1",
327+
"type": "string"
328+
}
329+
],
330+
"protocolProfileBehavior": {}
331+
}

app-middlewares/auth.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const express = require('express');
2+
const connection = require('../helpers/connection');
3+
const query = require('../helpers/query');
4+
5+
const router = express.Router();
6+
const dbConfig = require('../dbConfig');
7+
8+
router.post('/register', async (req, res) => {
9+
const { username, password } = req.body;
10+
const conn = await connection(dbConfig).catch(e => {});
11+
const user = await query(
12+
conn,
13+
`INSERT INTO USERS(username, password) VALUE(?, MD5(?));`,
14+
[username, password]
15+
);
16+
if (user.insertId) {
17+
res.send(await query(conn, `SELECT id, username FROM USERS WHERE ID=?`, [user.insertId]))
18+
return;
19+
}
20+
res.send({ id: null, username: null });
21+
});
22+
23+
router.post('/login', async (req, res) => {
24+
const { username, password } = req.body;
25+
const conn = await connection(dbConfig).catch(e => {});
26+
const user = await query(
27+
conn,
28+
`SELECT id, username FROM USERS WHERE username=? AND password=MD5(?)`,
29+
[username, password]
30+
);
31+
res.send(user[0] || { id: null, username: null });
32+
});
33+
34+
module.exports = router;

0 commit comments

Comments
 (0)