S39 Activity

master
Ron Reciproco 1 year ago
parent 8adc66c8ff
commit 5668d767e2

@ -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}.`)

@ -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?

@ -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');

@ -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}.`)
Loading…
Cancel
Save