From 0b91dcfba0a1ad9527e8b375fc0ceef648f98c86 Mon Sep 17 00:00:00 2001 From: Adarsh Dwivedi <152579045+adarshh8@users.noreply.github.com> Date: Thu, 23 Oct 2025 00:35:56 +0530 Subject: [PATCH] Refactor app.js to use ES6 modules and improve structure --- Full Stack Projects/todo-list/app.js | 81 ++++++++++++++++------------ 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/Full Stack Projects/todo-list/app.js b/Full Stack Projects/todo-list/app.js index 7136013d..74c6f218 100644 --- a/Full Stack Projects/todo-list/app.js +++ b/Full Stack Projects/todo-list/app.js @@ -1,48 +1,59 @@ -const express = require('express'); +import express from 'express'; +import bodyParser from 'body-parser'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const app = express(); -const bodyParser = require('body-parser'); -var i = 0; -var newItem = []; -var workItem = []; +const PORT = process.env.PORT || 3000; + +let personalItems = []; +let workItems = []; app.use(bodyParser.urlencoded({ extended: true })); -app.use(express.static('public')); -app.set('view engine', "ejs"); +app.use(express.static(path.join(__dirname, 'public'))); +app.set('view engine', 'ejs'); +// Helper function +const getFormattedDate = () => { + const today = new Date(); + const options = { weekday: 'long', day: 'numeric', month: 'long' }; + return today.toLocaleDateString('en-US', options); +}; + +// Routes app.get('/', (req, res) => { - var today = new Date(); - var options = { - weekday: "long", - day: "numeric", - month: "long" - }; - var day = today.toLocaleDateString('en-US', options); - - res.render('list', { listTitle: day, newListItem: newItem, i: i }); + res.render('list', { + listTitle: getFormattedDate(), + items: personalItems, + placeholder: 'Add a new task...', + route: '/' + }); }); -app.post("/", function (req, res) { - - let Item = req.body.item; - if (req.body.list === "Work") { - workItem.push(Item); - res.redirect('/work'); - } else { - - newItem.push(Item); - res.redirect("/"); - } +app.post('/', (req, res) => { + const item = req.body.item?.trim(); + if (item) personalItems.push(item); + res.redirect('/'); }); -app.get('/work', function (req, res) { - - res.render('list', { listTitle: "Work Item", newListItem: workItem }); - +app.get('/work', (req, res) => { + res.render('list', { + listTitle: '💼 Work List', + items: workItems, + placeholder: 'Add a new work task...', + route: '/work' + }); }); - - -app.listen(3000, () => { - console.log('Example app listening on port port!'); +app.post('/work', (req, res) => { + const item = req.body.item?.trim(); + if (item) workItems.push(item); + res.redirect('/work'); }); +app.listen(PORT, () => { + console.log(`🚀 Server running at http://localhost:${PORT}`); +});