├── package.json ├── index.js ├── index.html ├── .gitignore └── contact.html /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-folder", 3 | "version": "1.0.0", 4 | "description": "fynd assignment exercise 1", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Krithik Suvarna", 10 | "license": "ISC" 11 | } 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | const fs = require("fs"); 3 | const url = require("url"); 4 | const path = require("path"); 5 | const qs = require("querystring"); 6 | 7 | const server = http.createServer(); 8 | 9 | server.on("request", async (req, res) => { 10 | const parts = url.parse(req.url, true); 11 | 12 | res.writeHead(200, { "content-type": "text/html" }); 13 | switch (parts.pathname) { 14 | case "/message": 15 | if (req.method === "POST") { 16 | const buffers = []; 17 | for await (const chunk of req) { 18 | buffers.push(chunk); 19 | } 20 | const data = Buffer.concat(buffers).toString(); 21 | const message = qs.parse(data); 22 | const ws = fs.createWriteStream(path.join(__dirname, "messages.json"), { 23 | flags: "a+", 24 | }); 25 | ws.write(JSON.stringify(message)); 26 | } 27 | case "/contact": 28 | const rs = fs.createReadStream( 29 | path.join(__dirname, "contact.html"), 30 | "utf-8" 31 | ); 32 | rs.pipe(res); 33 | break; 34 | default: 35 | const rs1 = fs.createReadStream( 36 | path.join(__dirname, "index.html"), 37 | "utf-8" 38 | ); 39 | rs1.pipe(res); 40 | } 41 | }); 42 | 43 | server.on("error", (error) => { 44 | console.error(error.message); 45 | }); 46 | 47 | server.listen(3000); 48 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |Hi! I am Krithik Suvarna. I am a final year student studying at SIESGST, Nerul
41 |