Skip to content

Instantly share code, notes, and snippets.

View LautaroJayat's full-sized avatar
💭
👽

Lautaro Jayat LautaroJayat

💭
👽
View GitHub Profile
@LautaroJayat
LautaroJayat / index.js
Created January 26, 2020 15:39
Starting the server, Step 1
// 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(
@LautaroJayat
LautaroJayat / index.js
Created January 26, 2020 16:01
index.js - How a request object looks in NodeJS
// 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);
@LautaroJayat
LautaroJayat / index.js
Last active January 26, 2020 17:05
index.js - step 3 Sending data.
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!')
});
@LautaroJayat
LautaroJayat / index.js
Last active January 26, 2020 19:20
index.js - first encounter with a buffer
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;
@LautaroJayat
LautaroJayat / index.js
Created January 26, 2020 19:31
index.js reading the asynchronous way
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) => {
@LautaroJayat
LautaroJayat / index.js
Created January 26, 2020 19:58
index.js - Redirecting css and javascript calls
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.
@LautaroJayat
LautaroJayat / index.js
Created January 26, 2020 20:42
index.js Our final Server
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', () => {
@LautaroJayat
LautaroJayat / app.js
Created January 26, 2020 20:46
App.js - important module
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);
}
@LautaroJayat
LautaroJayat / controllers.js
Created January 26, 2020 20:52
Controllers.js
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();
}
@LautaroJayat
LautaroJayat / onlynumbers.js
Created January 27, 2020 18:25
Array Question
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