good-v 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/good.js +399 -0
  2. package/index.js +0 -0
  3. package/package.json +7 -0
package/good.js ADDED
@@ -0,0 +1,399 @@
1
+ server.js
2
+
3
+ // express server
4
+ const express = require("express");
5
+ const mongoose = require("mongoose");
6
+ const session = require("express-session");
7
+
8
+ const app = express();
9
+
10
+ // form ka data read karne ke liye
11
+ app.use(express.urlencoded({ extended: true }));
12
+
13
+ // session setup (login state yaad rakhne ke liye)
14
+ app.use(
15
+ session({
16
+ secret: "secretkey", // security key
17
+ resave: false,
18
+ saveUninitialized: false,
19
+ })
20
+ );
21
+
22
+ // EJS as view engine
23
+ app.set("view engine", "ejs");
24
+
25
+ // MongoDB connect
26
+ mongoose.connect("mongodb://127.0.0.1:27017/ecommerce");
27
+
28
+ // models import
29
+ const User = require("./models/User");
30
+ const Product = require("./models/Product");
31
+
32
+
33
+ // ---------------- SIGNUP ----------------
34
+
35
+ // signup page
36
+ app.get("/signup", (req, res) => {
37
+ res.render("signup");
38
+ });
39
+
40
+ // signup logic
41
+ app.post("/signup", async (req, res) => {
42
+ const user = new User(req.body); // user create
43
+ await user.save(); // database me save
44
+ res.redirect("/login");
45
+ });
46
+
47
+
48
+ // ---------------- LOGIN ----------------
49
+
50
+ // login page
51
+ app.get("/login", (req, res) => {
52
+ res.render("login");
53
+ });
54
+
55
+ // login logic
56
+ app.post("/login", async (req, res) => {
57
+ const user = await User.findOne(req.body);
58
+
59
+ if (user) {
60
+ req.session.userId = user._id; // session set
61
+ res.redirect("/dashboard");
62
+ } else {
63
+ res.send("Invalid credentials");
64
+ }
65
+ });
66
+
67
+
68
+ // ---------------- DASHBOARD (READ) ----------------
69
+
70
+ app.get("/dashboard", async (req, res) => {
71
+ // bina login access nahi
72
+ if (!req.session.userId) {
73
+ return res.redirect("/login");
74
+ }
75
+
76
+ const products = await Product.find(); // READ
77
+ res.render("dashboard", { products });
78
+ });
79
+
80
+
81
+ // ---------------- CREATE ----------------
82
+
83
+ app.post("/add-product", async (req, res) => {
84
+ const product = new Product(req.body); // product create
85
+ await product.save(); // database me save
86
+ res.redirect("/dashboard");
87
+ });
88
+
89
+
90
+ // ---------------- DELETE ----------------
91
+
92
+ app.get("/delete/:id", async (req, res) => {
93
+ await Product.findByIdAndDelete(req.params.id);
94
+ res.redirect("/dashboard");
95
+ });
96
+
97
+
98
+ // ---------------- UPDATE ----------------
99
+
100
+ // edit page
101
+ app.get("/edit/:id", async (req, res) => {
102
+ const product = await Product.findById(req.params.id);
103
+ res.render("edit", { product });
104
+ });
105
+
106
+ // update logic
107
+ app.post("/edit/:id", async (req, res) => {
108
+ await Product.findByIdAndUpdate(req.params.id, req.body);
109
+ res.redirect("/dashboard");
110
+ });
111
+
112
+
113
+ // ---------------- LOGOUT ----------------
114
+
115
+ app.get("/logout", (req, res) => {
116
+ req.session.destroy(); // session end
117
+ res.redirect("/login");
118
+ });
119
+
120
+
121
+ // server start
122
+ app.listen(3000, () => {
123
+ console.log("Server running on port 3000");
124
+ });
125
+
126
+
127
+ models
128
+ user.js
129
+ const mongoose = require("mongoose");
130
+
131
+ // user ka structure
132
+ const userSchema = new mongoose.Schema({
133
+ name: String,
134
+ email: String,
135
+ password: String,
136
+ });
137
+
138
+ module.exports = mongoose.model("User", userSchema);
139
+
140
+
141
+ models
142
+ product.js
143
+ const mongoose = require("mongoose");
144
+
145
+ // product ka structure
146
+ const productSchema = new mongoose.Schema({
147
+ name: String,
148
+ price: Number,
149
+ });
150
+
151
+ module.exports = mongoose.model("Product", productSchema);
152
+
153
+
154
+ dashboard.ejs
155
+ <h1>Dashboard</h1>
156
+
157
+ <a href="/logout">Logout</a>
158
+
159
+ <!-- CREATE -->
160
+ <form action="/add-product" method="post">
161
+ <input name="name" placeholder="Product Name" />
162
+ <input name="price" placeholder="Price" />
163
+ <button>Add</button>
164
+ </form>
165
+
166
+ <!-- READ + DELETE + UPDATE -->
167
+ <ul>
168
+ <% products.forEach(p => { %>
169
+ <li>
170
+ <%= p.name %> - <%= p.price %>
171
+ <a href="/edit/<%= p._id %>">Edit</a>
172
+ <a href="/delete/<%= p._id %>">Delete</a>
173
+ </li>
174
+ <% }) %>
175
+ </ul>
176
+
177
+
178
+ edit.ejs
179
+ <h2>Edit Product</h2>
180
+
181
+ <form action="/edit/<%= product._id %>" method="post">
182
+ <input name="name" value="<%= product.name %>" />
183
+ <input name="price" value="<%= product.price %>" />
184
+ <button>Update</button>
185
+ </form>
186
+
187
+
188
+ jwttt
189
+
190
+ user.js
191
+ const mongoose = require("mongoose");
192
+
193
+ // user ka structure
194
+ const userSchema = new mongoose.Schema({
195
+ name: String,
196
+ email: String,
197
+ password: String,
198
+ });
199
+
200
+ module.exports = mongoose.model("User", userSchema);
201
+
202
+ blog.js
203
+ const mongoose = require("mongoose");
204
+
205
+ // blog ka structure
206
+ const blogSchema = new mongoose.Schema({
207
+ title: String,
208
+ content: String,
209
+ userId: String, // kis user ne likha
210
+ });
211
+
212
+ module.exports = mongoose.model("Blog", blogSchema);
213
+
214
+
215
+
216
+
217
+
218
+ middleware.js
219
+ const jwt = require("jsonwebtoken");
220
+
221
+ // token check karne ke liye
222
+ function auth(req, res, next) {
223
+ const token = req.headers["authorization"];
224
+
225
+ if (!token) {
226
+ return res.send("Token missing");
227
+ }
228
+
229
+ try {
230
+ const decoded = jwt.verify(token, "secretkey");
231
+ req.userId = decoded.id; // user id save
232
+ next();
233
+ } catch {
234
+ res.send("Invalid token");
235
+ }
236
+ }
237
+
238
+ module.exports = auth;
239
+
240
+
241
+
242
+ server.js
243
+ const jwt = require("jsonwebtoken");
244
+
245
+ // token check karne ke liye
246
+ function auth(req, res, next) {
247
+ const token = req.headers["authorization"];
248
+
249
+ if (!token) {
250
+ return res.send("Token missing");
251
+ }
252
+
253
+ try {
254
+ const decoded = jwt.verify(token, "secretkey");
255
+ req.userId = decoded.id; // user id save
256
+ next();
257
+ } catch {
258
+ res.send("Invalid token");
259
+ }
260
+ }
261
+
262
+ module.exports = auth;
263
+
264
+
265
+ dashboard.html
266
+ const jwt = require("jsonwebtoken");
267
+
268
+ // token check karne ke liye
269
+ function auth(req, res, next) {
270
+ const token = req.headers["authorization"];
271
+
272
+ if (!token) {
273
+ return res.send("Token missing");
274
+ }
275
+
276
+ try {
277
+ const decoded = jwt.verify(token, "secretkey");
278
+ req.userId = decoded.id; // user id save
279
+ next();
280
+ } catch {
281
+ res.send("Invalid token");
282
+ }
283
+ }
284
+
285
+ module.exports = auth;
286
+
287
+
288
+
289
+ routing simple
290
+ const express = require("express");
291
+ const app = express();
292
+
293
+ // home route
294
+ app.get("/", (req, res) => {
295
+ res.send("Home Page");
296
+ });
297
+
298
+ // about route
299
+ app.get("/about", (req, res) => {
300
+ res.send("About Page");
301
+ });
302
+
303
+ app.listen(3000);
304
+
305
+
306
+ // CREATE
307
+ app.post("/product", (req, res) => {
308
+ res.send("Product added");
309
+ });
310
+
311
+ // READ
312
+ app.get("/product", (req, res) => {
313
+ res.send("All products");
314
+ });
315
+
316
+ // UPDATE
317
+ app.put("/product/:id", (req, res) => {
318
+ res.send("Product updated");
319
+ });
320
+
321
+ // DELETE
322
+ app.delete("/product/:id", (req, res) => {
323
+ res.send("Product deleted");
324
+ });
325
+
326
+ advanced
327
+
328
+ const express = require("express");
329
+ const router = express.Router();
330
+
331
+ router.get("/login", (req, res) => {
332
+ res.send("Login page");
333
+ });
334
+
335
+ module.exports = router;
336
+
337
+ using crud
338
+
339
+
340
+ http
341
+
342
+ const http = require("http");
343
+
344
+ const server = http.createServer((req, res) => {
345
+
346
+ if (req.url === "/") {
347
+ res.writeHead(200, { "Content-Type": "text/plain" });
348
+ res.end("Home Page");
349
+ }
350
+ else if (req.url === "/html") {
351
+ res.writeHead(200, { "Content-Type": "text/html" });
352
+ res.end("<h1>HTML Page</h1>");
353
+ }
354
+ else if (req.url === "/json") {
355
+ res.writeHead(200, { "Content-Type": "application/json" });
356
+ res.end(JSON.stringify({ msg: "JSON Response" }));
357
+ }
358
+ else {
359
+ res.writeHead(404, { "Content-Type": "text/plain" });
360
+ res.end("Page Not Found");
361
+ }
362
+
363
+ });
364
+
365
+ server.listen(3000);
366
+
367
+
368
+
369
+ const fs = require("fs");
370
+
371
+ // 1️⃣ WRITE (Create file)
372
+ fs.writeFile("demo.txt", "Hello Node.js", (err) => {
373
+ if (err) throw err;
374
+ console.log("File created");
375
+
376
+ // 2️⃣ READ
377
+ fs.readFile("demo.txt", "utf8", (err, data) => {
378
+ if (err) throw err;
379
+ console.log("File content:", data);
380
+
381
+ // 3️⃣ APPEND
382
+ fs.appendFile("demo.txt", "\nThis is appended text", (err) => {
383
+ if (err) throw err;
384
+ console.log("Data appended");
385
+
386
+ // 4️⃣ UPDATE (overwrite)
387
+ fs.writeFile("demo.txt", "Updated content", (err) => {
388
+ if (err) throw err;
389
+ console.log("File updated");
390
+
391
+ // 5️⃣ DELETE
392
+ fs.unlink("demo.txt", (err) => {
393
+ if (err) throw err;
394
+ console.log("File deleted");
395
+ });
396
+ });
397
+ });
398
+ });
399
+ });
package/index.js ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "good-v",
3
+ "version": "1.0.0",
4
+ "description": "backend reference code",
5
+ "author": "romram",
6
+ "license": "MIT"
7
+ }