This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First we require the http native module and we store it in a variable | |
const http = require('http'); | |
// Then we set a port based on the following condition: if the enviroment variable | |
// 'PORT' exists, then we use it. Else, we use 3000. | |
const PORT = process.env.PORT || 3000; | |
// Then we call the createServer() function to initialize our server | |
// And we store it in a cool variable. | |
const server = http.createServer( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Now we put assign the names for the request and response objects that | |
// are used in the createServer(function); | |
const server = http.createServer((req, res) => { | |
// Lets see what a request object looks like. | |
console.log(req); | |
}).listen(PORT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const server = http.createServer((req, res) => { | |
// Let store our URL and Method in a variable | |
const URL = req.url; | |
// Lets make something using conditionals | |
if (req.url === '/') { | |
// Note that we are using res.write() method and then using res.end(). | |
// This is mandatory if you expect your server to work fine. | |
res.write('Yea! Its Working! Esoteric Knowledge!', 'utf-8', () => { | |
console.log('console.log from the callback function!') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
// Require the File System Module | |
const fs = require('fs'); | |
const PORT = process.env.PORT || 3000; | |
const server = http.createServer((req, res) => { | |
const URL = req.url; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const server = http.createServer((req, res) => { | |
const URL = req.url; | |
if (req.url === '/') { | |
// We try to read the method in a Synchronous way | |
// Note that we dont assign the output to a variable, | |
// instead, we store the output in the second parameter of the | |
// callback function. | |
fs.readFile('./views/index.html', (err, data) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const server = http.createServer((req, res) => { | |
const URL = req.url; | |
if (req.url === '/') { | |
// We try to read the method in a Synchronous way | |
// Note that we dont assign the output to a variable, | |
// instead, we store the output in the second parameter of the | |
// callback function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
// This is what we are importing as a function to http.createServer() | |
const app = require('./app'); | |
const PORT = process.env.PORT || 3000; | |
var server = http.createServer(app).listen(PORT); | |
server.on('listening', () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ctrl = require('./controllers/controllers'); | |
const app = (req, res) => { | |
const URL = req.url; | |
// What we say when the request method isnt GET | |
if (req.method !== 'GET') { | |
ctrl.forInvalidMethod(req, res); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const fs = require('fs'); | |
const ctrl = {}; | |
ctrl.forInvalidMethod = function (req, res) { | |
res.statusCode = 405; | |
res.setHeader('Allow', 'GET'); | |
return res.end(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [1, 2, 3, '4', undefined, 'a', [], null]; | |
function onlyNumbers(arr) { | |
// We create an array to output | |
let output = []; | |
// We use forEach() to check if the type of every element is the string 'number' | |
// because we know that it outputs a lowercase string with the type. | |
// If true, we push the element to the output. | |
arr.forEach(e => { if (typeof (e) === 'number') { output.push(e) } }) | |
return output |
OlderNewer