├── .gitignore ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "cors": "^2.8.5", 13 | "express": "^4.18.2" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const bodyParser = require("body-parser"); 3 | const app = express(); 4 | app.use(bodyParser.urlencoded({extended:false})) 5 | app.use(bodyParser.json()); 6 | const cors = require("cors"); 7 | app.use(cors()); 8 | const port = 5000; 9 | const data ={ 10 | name:"prashant", 11 | age:21, 12 | gender:"male", 13 | nationality:"nepali", 14 | status:"present", 15 | }; 16 | 17 | const heavy = { 18 | gender: "male", 19 | class : "S", 20 | number: 654564, 21 | video: "https://apivideo-demo.s3.amazonaws.com/hello.mp4", 22 | } 23 | 24 | app.get("/msg",(req,res)=>{ 25 | 26 | res.send(data); 27 | }) 28 | 29 | 30 | app.get("/heavy",(req,res)=>{ 31 | res.send(heavy); 32 | }) 33 | 34 | 35 | const contact_success = { 36 | message: "Form submitted successfully" 37 | }; 38 | 39 | /*POST method for the contact form */ 40 | app.post("/contact",(req,res)=>{ 41 | const data = req.body; 42 | console.log(data); 43 | res.send(contact_success) 44 | // res.send("Send Successfull for contact form"); 45 | }); 46 | 47 | const success = { 48 | msg: "Login Successfully", 49 | token : "superSecretToken", 50 | }; 51 | 52 | const success1 = { 53 | msg: "Signed in Successfully", 54 | token : "superSecretToken", 55 | }; 56 | 57 | /*For Login and its validation*/ 58 | app.post("/login",(req,res)=>{ 59 | const data = req.body; 60 | if(req.body.username && req.body.password){ 61 | if(req.body.username === "prashant@gmail.com"){ 62 | if(req.body.password === "helloguys"){ 63 | res.send(success); 64 | }else{ 65 | res.status(401).send("Password not matched") 66 | } 67 | } 68 | else{ 69 | res.status(404).send("User doesn't exist") 70 | } 71 | 72 | }else { 73 | res.status(422).send("Username or password missing") 74 | } 75 | 76 | }) 77 | 78 | /*For SignUp and its validation*/ 79 | app.post("/signup", (req, res) => { 80 | const {username,password,confirmPassword} = req.body; 81 | 82 | // Regular expressions for username and password validation 83 | const usernameRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 84 | const passwordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/; 85 | 86 | if (req.body.username && req.body.password) { 87 | if (usernameRegex.test(req.body.username)) { 88 | if (passwordRegex.test(req.body.password)) { 89 | if (req.body.username !== "prashant@gmail.com") { 90 | res.send(success1); 91 | } else { 92 | res.status(404).send("Username already exists please try another") 93 | } 94 | } else { 95 | res.status(401).send("Password must be between 6 to 20 characters and contain at least one numeric digit, one uppercase and one lowercase letter") 96 | } 97 | } else { 98 | res.status(401).send("Invalid email format") 99 | } 100 | } else { 101 | res.status(422).send("Username or password missing") 102 | } 103 | }); 104 | 105 | /* For Dynamic routing */ 106 | 107 | const dynamicData = [ 108 | {name:"prashant", roll:"1"}, 109 | {name:"sushan", roll:"2"}, 110 | {name:"milan", roll:"3"}, 111 | 112 | ]; 113 | console.log([dynamicData[0]]) 114 | 115 | app.get("/dynamic/:id",(req,res)=>{ 116 | const id = req.params.id; 117 | if(isNaN(id)){ 118 | res.status(442).send("Unprocessable entity") 119 | }else{ 120 | if(1 <=id && id<=dynamicData.length){ 121 | res.send(dynamicData[id-1]); 122 | }else{ 123 | res.status(404).send("Data not found"); 124 | } 125 | } 126 | 127 | }) 128 | 129 | /* Dynamic for about page */ 130 | const dynamicAbout = [ 131 | {name:"anish", roll:"1"}, 132 | {name:"sushant", roll:"2"}, 133 | {name:"sakshyam", roll:"3"}, 134 | 135 | ]; 136 | console.log([dynamicAbout[0]]) 137 | 138 | app.get("/about/:aboutid",(req,res)=>{ 139 | const aboutid = req.params.aboutid; 140 | if(isNaN(aboutid)){ 141 | res.status(442).send("Unprocessable entity") 142 | }else{ 143 | if(1 <=aboutid && aboutid<=dynamicAbout.length){ 144 | res.send(dynamicAbout[aboutid-1]); 145 | }else{ 146 | res.status(404).send("Data not found"); 147 | } 148 | } 149 | 150 | }) 151 | 152 | 153 | /* Post for the react hook form */ 154 | 155 | const form_success = { 156 | message: "Form submitted successfully haii" 157 | }; 158 | // Route for form submission (replace with your actual form endpoint) 159 | app.post("/reacthookform",(req,res)=>{ 160 | const data = req.body; 161 | console.log(data); 162 | // res.send(form_success) 163 | // res.send("Send Successfull for react hook form"); 164 | 165 | const errors = []; // Array to store error messages 166 | 167 | const { name, email, password, gender, hobbies, message } = req.body; 168 | 169 | // Name validation 170 | if (!name || name.trim() === '') { 171 | errors.push('Name is required'); 172 | } 173 | 174 | // Email validation (basic check) 175 | if (!email || !email.includes('@') || !email.includes('.')) { 176 | errors.push('Invalid email format'); 177 | } 178 | 179 | // Password validation (basic check) 180 | if (!password || password.length < 8) { 181 | errors.push('Password must be at least 8 characters long'); 182 | } 183 | 184 | // Gender validation 185 | if (!gender || !['male', 'female', 'other'].includes(gender)) { 186 | errors.push('Invalid gender'); 187 | } 188 | 189 | // Hobbies validation (basic check) 190 | // if (!hobbies || hobbies.length === 0) { 191 | // errors.push('Select at least one hobby'); 192 | // } 193 | 194 | // Message validation 195 | if (!message || message.trim() === '') { 196 | errors.push('Message is required'); 197 | } 198 | 199 | if (errors.length > 0) { 200 | // Send bad request with error messages 201 | return res.status(400).json({ errors }); 202 | } 203 | 204 | // Process validated form data 205 | console.log(`Name: ${name}, Email: ${email}, Password: ${password} (hidden)`); 206 | console.log(`Gender: ${gender}, Hobbies: ${hobbies}, Message: ${message}`); 207 | 208 | // res.send({ message: 'Form submitted successfully!' }); 209 | res.send(data); 210 | }); 211 | 212 | 213 | 214 | 215 | /* GET method for table in form */ 216 | const detailsTable = [ 217 | {name:"peter", roll:"101", grade: "A"}, 218 | {name:"ben", roll:"202", grade : "B+"}, 219 | {name:"sid", roll:"303", grade : "A+"}, 220 | {name:"hulk", roll:"404", grade : "C+"}, 221 | {name:"tommy", roll:"505", grade : "B+"}, 222 | {name:"mike", roll:"606", grade : "A"}, 223 | {name:"john", roll:"707", grade : "B"}, 224 | 225 | 226 | ]; 227 | console.log([detailsTable[0]]) 228 | 229 | app.get("/tableData",(req,res)=>{ 230 | 231 | res.send(detailsTable); 232 | 233 | }) 234 | 235 | /* GET method for material-react-table*/ 236 | 237 | const matTable = [ 238 | { 239 | name: { 240 | firstName: "John", 241 | lastName: "Doe", 242 | }, 243 | address: "261 Erdman Ford", 244 | city: "East Daphne", 245 | state: "Kentucky", 246 | }, 247 | { 248 | name: { 249 | firstName: "Jane", 250 | lastName: "Doe", 251 | }, 252 | address: "769 Dominic Grove", 253 | city: "Columbus", 254 | state: "Ohio", 255 | }, 256 | { 257 | name: { 258 | firstName: "Joe", 259 | lastName: "Doe", 260 | }, 261 | address: "566 Brakus Inlet", 262 | city: "South Linda", 263 | state: "West Virginia", 264 | }, 265 | { 266 | name: { 267 | firstName: "Kevin", 268 | lastName: "Vandy", 269 | }, 270 | address: "722 Emie Stream", 271 | city: "Lincoln", 272 | state: "Nebraska", 273 | }, 274 | { 275 | name: { 276 | firstName: "Joshua", 277 | lastName: "Rolluffs", 278 | }, 279 | address: "32188 Larkin Turnpike", 280 | city: "Charleston", 281 | state: "South Carolina", 282 | }, 283 | ]; 284 | 285 | console.log([matTable[0]]) 286 | 287 | app.get("/mattable",(req,res)=>{ 288 | 289 | res.send(matTable); 290 | 291 | }) 292 | 293 | /* --- */ 294 | app.listen(port,()=>{ 295 | console.log(`Listening to port ${port}`); 296 | }); 297 | 298 | 299 | --------------------------------------------------------------------------------