From 5668d767e2f5d3e16e10d44c88fea011e0fec020 Mon Sep 17 00:00:00 2001 From: ronreciproco123 Date: Tue, 24 Oct 2023 20:36:35 +0800 Subject: [PATCH] S39 Activity --- individual/backend/s39/activity/index.js | 14 +++++++++ individual/backend/s39/activity/quiz.js | 11 +++++++ individual/backend/s39/discussion/index.js | 32 +++++++++++++++++++ individual/backend/s39/discussion/routes.js | 35 +++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 individual/backend/s39/activity/index.js create mode 100644 individual/backend/s39/activity/quiz.js create mode 100644 individual/backend/s39/discussion/index.js create mode 100644 individual/backend/s39/discussion/routes.js diff --git a/individual/backend/s39/activity/index.js b/individual/backend/s39/activity/index.js new file mode 100644 index 0000000..3fdc537 --- /dev/null +++ b/individual/backend/s39/activity/index.js @@ -0,0 +1,14 @@ +const http = require('http') +const port = 3000 + +const app = http.createServer((request, response) => { + if (request.url == '/login') { + response.writeHead(200, {'content-type': 'text/plain'}) + response.end("Welcome to the login page") + } else { + response.writeHead(404, {'content-type': 'text/plain'}) + response.end("I'm sorry, the page you are looking for cannot be found") + } +}) +app.listen(port) +console.log(`Server now accesible at localhost:${port}.`) \ No newline at end of file diff --git a/individual/backend/s39/activity/quiz.js b/individual/backend/s39/activity/quiz.js new file mode 100644 index 0000000..e3133ca --- /dev/null +++ b/individual/backend/s39/activity/quiz.js @@ -0,0 +1,11 @@ +// 1. What directive is used by Node.js in loading the modules it needs? +- Require +// 2. What Node.js module contains a method for server creation? +- http +// 3. What is the method of the http object responsible for creating a server using Node.js? +- http.createServer +// 4. What method of the response object allows us to set status codes and content types? + +// 5. Where will console.log() output its contents when run in Node.js? + +// 6. What property of the request object contains the address's endpoint? \ No newline at end of file diff --git a/individual/backend/s39/discussion/index.js b/individual/backend/s39/discussion/index.js new file mode 100644 index 0000000..c0ff70f --- /dev/null +++ b/individual/backend/s39/discussion/index.js @@ -0,0 +1,32 @@ +// Use the "require" directive to load node.js modules +// A module is a software component or part of a program that contains one or more routines +// The "http module" it lets Node.js transfer data using the Hyper Text Transfer Protocol +// The "http module" is a set of individual files that contain code to create a "component" that helps establish data transfer between applications. +// HTTP is a protocol that allows the fetching of resources suchh as HTML documents +// Clients (browser) and servers (nodeJS/express JS applications) communicate by exchanging individual messages. +// The messages sent by the "client", usually a web broswer, are called "request" +// The messages sent by the "server" as an answer are called "responses" +let http = require("http"); + + +// Using this module's createServer() method, we can create an HTTP server that listens to requests on a specified port and gives responses back to the client +// The http module has a createServer() method that accepts a function as an argument and allows for a creation of a server +// The arguments passed in the function are request and response object (data type) that contains methods that allow us to receive requests from the client and send responses back to it +// A port is a virtual point where network connections start and end. +// Each port is associated with a specific process or service +// The server will be assigned to port 4000 via the "listen(4000)" method where communicating with our server happens. + +http.createServer(function (request, response) { + + // Use writeHead() method to: + // Set a status code for the response - a 200 means OK + // Set the content-type of the response as a plain text message + response.writeHead(200, {'Content-Type': 'text/plain'}); + + // Send the response with text content 'Hello World' + response.end('Hello World'); + +}).listen(4000) + +// When server is running, console will +console.log('Server running at localhost:4000'); \ No newline at end of file diff --git a/individual/backend/s39/discussion/routes.js b/individual/backend/s39/discussion/routes.js new file mode 100644 index 0000000..aa6cb76 --- /dev/null +++ b/individual/backend/s39/discussion/routes.js @@ -0,0 +1,35 @@ +const http = require('http') + +// Creates a variable 'port' to store the port number +const port = 4000 + +// Creates a variable 'app' that stores the output of the 'createServer' method +const app = http.createServer((request, response) => { + + // Accessing the 'greeting' route returns a message of 'Hello World' + if (request.url == '/greeting') { + + response.writeHead(200, {'Content-Type': 'text/plain'}) + response.end('Hello World') + + // Accesing the 'homepage' route returns a message of 'This is the homepage' + } else if (request.url == '/homepage') { + + response.writeHead(200, {'Content-Type': 'text/plain'}) + response.end('This is the homepage') + + // All other routes will return a message of 'Page not available' + } else { + + // Set a status code for the response - a 404 means Not Found + response.writeHead(404, {'Content-Type': 'text/plain'}) + response.end('Page not available') + } + +}) + +// Uses the "app" and "port" variables created above. +app.listen(port) + +// When server is running, console will print the message: +console.log(`Server now accesible at localhost:${port}.`) \ No newline at end of file