Commit b156d8c2 authored by Muhammad Ameen's avatar Muhammad Ameen 💻

Node js Learnning

parents
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
//* Core Node JS
// const http = require("http");
// const fs = require("fs");
// const path = require("path");
// const filePath = path.join(process.cwd(), "data.txt");
// const server = http.createServer((req, res) => {
// if (req.url === "/") {
// res.write("Hello /");
// res.end();
// } else if (req.url === "/form") {
// res.setHeader("Content-Type", "text/html");
// res.write(
// "<form action='/submit' method='POST'><input name='data' /><button>Submit</button></form>"
// );
// res.end();
// } else if (req.url === "/submit") {
// let allData = "";
// req.on("data", (chunk) => (allData += chunk));
// req.on("end", () => {
// fs.readFile(filePath, "utf8", (err, fileData) => {
// const newData = fileData + "\n" + allData;
// fs.writeFile(filePath, newData, () => {
// res.write("Form Submit Success!");
// console.log(allData);
// res.end();
// });
// });
// });
// }
// });
// server.listen(3200);
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const form = require("./routes/form");
const path = require("path");
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(bodyParser.json());
app.use(express.static(path.join(process.cwd(), "public")));
app.set("view engine", "ejs");
app.set("views", "views");
app.use("/form", form);
// app.use((req, res, next) => {
// req.data = "Ameen";
// next();
// });
// app.use((req, res) => {
// res.send(req.data);
// });
app.listen(3002);
This diff is collapsed.
{
"name": "nodejs",
"version": "1.0.0",
"description": "Basic Node app",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "Muhammad Ameen",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"ejs": "^3.1.9",
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
body {
background-color: black;
}
h2 {
color: white;
}
const express = require("express");
const router = express.Router();
const path = require("path");
router.get("/", (req, res) => {
// res.sendFile(path.join(process.cwd(), "views", "form.ejs"));
res.render("form", { user: req.query.name });
setTimeout(() => {}, 3000);
});
router.get("/", (req, res) => {
res.send(`
<form method="post" action="/form/submit">
<input name="data" />
<button>Submit</button>
</form>`);
});
router.post("/submit", (req, res) => {
res.send(req.body);
console.log("Form Submission", req.body);
});
module.exports = router;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Form</title>
<link rel="stylesheet" href="/css/style.css" />
</head>
<body>
<h2>Hello Welcome to ejs <%= user %></h2>
<form action="/form/submit" method="post">
<input type="email" name="email" />
<input type="password" name="password" />
<button type="submit">Submit</button>
</form>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment