Skip to content

updated index-with-require/requestListener #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions CD220Labs/http_server/index-with-require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Import the HTTP module
// Import the HTTP module
const http = require('http');

// Import the 'today' module
Expand All @@ -7,8 +7,24 @@ const today = require('./today');
// Define the request listener function
const requestListener = function (req, res) {
res.writeHead(200); // Set the status code to 200 (OK)
let hour = today.getHours();
let date = today.getDateObject().toLocaleString('en-US', { timeZone: 'America/New_York' }).slice(0, 9);

let greeting = 'Food is important!';
if (hour > 7 && hour < 12) {
greeting = 'Have you had breakfast yet?';
} else if (hour >= 12 && hour < 15) {
greeting = 'Have you had lunch yet?';
} else if (hour >= 15 && hour < 19) {
greeting = 'Have you had a snack yet?';
} else if (hour >= 19 && hour < 23) {
greeting = 'Have you had dinner yet?';
} else {
greeting = 'Its late, you should be sleeping!';
}

// Send the response with the current date from the 'today' module
res.end(`Hello, World! The date today is ${today.getDate()}`);
res.end(`Hello, World! \nThe date today is ${date} \n${greeting}`);
};

// Define the port number
Expand Down
4 changes: 2 additions & 2 deletions CD220Labs/http_server/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Import the HTTP module
// Import the HTTP module
const http = require('http');

// Define the request listener function
const requestListener = function (req, res) {
res.writeHead(200); // Set the status code to 200 (OK)
res.end('Hello, World!'); // Send the response "Hello, World!"
res.end('Hello, World!\n'); // Send the response "Hello, World!"
};

// Define the port number
Expand Down
19 changes: 18 additions & 1 deletion CD220Labs/http_server/today.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// Export a function named 'getDate' from the module
/*
// Export a function named 'getDate' from the module
module.exports.getDate = function getDate() {
// Get the current date and time in the timezone "Australia/Brisbane"
let aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
return aestTime; // Return the formatted date and time
};
*/
module.exports = {
getDate: function getDate() {
return new Date().toLocaleString("en-US", { timeZone: "America/New_York" });
},
getHours: function getHour() {
let dateObj = new Date().toLocaleString('en-US', { timeZone: 'America/New_York' });
let tempDate = new Date(dateObj);
return tempDate.getHours();
},
getDateObject: function getDateObject() {
let dateObj = new Date();
let aestTime = new Date(dateObj.toLocaleString('en-US', { timeZone: 'America/New_York' }));
return aestTime;
}
};