Post/Code

HomeAboutUsesNow

PowerShell, Plain JS Post, Enabling CORS on Express and Express Body Parser.

  • Determine the version of PowerShell you are running: $PSVersionTable.PSVersion
  • Determine the execution policy on PowerShell (this is necessary to determine whether you can ru PowerShell scripts): Get-ExecutionPolicy
  • Useful resource for setting up Invoke-RestMethod calls via PowerShell scripts | Link
  • Enable CORS on an Express server | Link

    app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
    });
    
  • To use req.body inside of Express you need to load Body Parser | Link
  • First install it: npm i body-parser

    var express = require('express);
    var bodyParser = require('body-parser');
    

var app = express();

app.use(bodyParser.json());

app.post('/', function(req, res) { console.log(req.body); }) app.listen(8000);

* In package.json check: "main": "app.js", then you can just run: nodemon instead of: nodemon app.js * npm i pug --save * mkdir view && cd_$ * touch index.pug * Then:
    app.set('view engine', 'pug');
    ..
    app.get('/', (req, res) => {
        res.render('index', {title='Hello world!'});
    });
* Pug templating 101 (Assume we have var colors = ['red', 'blue', 'green'])
    doctype html
    html(lang="en")
        head
            title= title
        body
            div.wrapper
            .content
            ul
                each color in colors
                   li color
            p#main Hi!
            #secondary
            if hint 
                p
                    i Hint: #{hint}
            else 
                p (There is no hint)
* Pug locals
    app.get('/cards', (req, res) => {
        res.locals.prompt = "What is your question?";
        res.locals.hint = "Think about something."; // interpolated from #{hint} above
        res.render('card');
        // res.render('card', {prompt: 'What is your...', hint: 'Think about...'});
    });
* Pug Partials
In the layout.pug file (along with a: mkdir includes)
...
header 
    include includes/header.pug
...
block content
...
    include includes/footer.pug
In the index.pug file:
extends layout.pug

block content section#wrapper h1 Hello world!

Eloquent JavaScript Chapter 4: Data Structures

  • Almost all JavaScript values have properties except null and undefined.
  • value.prop prop must be a valid variable name and directly names the propery.
  • value[prop] prop is evaluated to get the property name.
  • The elements of an array are stored in properties with numbers for their names.
  • Properties that contain functions are called methods. Examples are .toUppercase, .toLowerCase for strings. Array have pop(), push(), .join() amongst others.
  • You can delete an object property with: delete object.propertyName;
  • console.log("propertyName" in object); // false
  • array.shift(); will return (and remove) the first item in the array, as opposed to .pop() which will return and remove the last item in the array.
  • Similarly .unshift() add an item to the beginning of an array and .push() adds an item to the end of an array.
  • indexOf(x) returns the index of the first instance of x in the array whereas lastIndexOf(x) returns the index of the last instance of x in the array. Both take an optional second argument to indicate where to start the search from.
  • .slice() takes two parameters, start(inclusive), end(exclusive) and returns an array of the items between start and end. If no second parameter is supplied, it takes all the items after the start.

    console.log([0, 1, 2, 3, 4].slice(2, 4));
    console.log([0, 1, 2, 3, 4].slice(2));
    
  • array1.concat(array2); will put two arrays into one and return the resulting array.
  • " somestring \n".trim(); will remove all the whitespace (spaces, tabs, newlines) from the string
  • Strings, numbers and booleans are not objects and are immutable which means you cannot add properties to them.
  • Whenever a function is called, a special variable names 'arguments' is added to the environment in which the function body runs. arguments has a .length property.
  • Math.random();

    console.log(Math.floor(Math.random()*10));
    
  • All global variables are stored in the global object, that is window.

    var myVar = 10;
    console.log("myVar" in window);
    console.log(window.myVar);