fabic974 random (binary) wanderings in the philament empires…

JavaScript - notes

Language notes

Frameworks

You don’t need jQuery (anymore)

PWAs

PWAs : Pointers

Promises

async-await lambda IIFE
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});
  await browser.close();
})();

Promise.all([])

Client-side

  • document.querySelectorAll("a.nav[rel=next][href^='http://example.com/abc/']")

Node.js

ExpressJS

Project setup

From :

$ mkcd express-01
$ npm init
$ npm install express --save

Most basic hello world example

From :

$ cat >> index.js <<EOF
const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))
EOF

Basic setup with React express view.

https://github.com/reactjs/express-react-views

$ express --view twig --css sass

todo…

Basic setup with Vue.js

https://github.com/express-vue/express-vue

React

Feathers.js

Vue.js

$ npm install -g vue-cli
$ vue init webpack vuejs-01
? Project name vuejs-01
? Project description fabic.net Vue.js experiments (01)
? Author Fabien Cadet <fabi@winterfell.fabic.net>
? Vue build standalone
? Install vue-router? Yes
? Use ESLint to lint your code? Yes
? Pick an ESLint preset Standard
? Set up unit tests Yes

Lifecycle diagram

Compilers & static types.

Static (compile-time) type checking

Webpack

Setup

Other (todo)

Node.js packages

Other

Deployment

install-nodejs-latest.sh

todo: setup paths

Install GCC/G++ so that NPM can build stuff :

sudo apt-get install g++

Using StrongLoop’s Process Manager :

npm install -g strongloop

Articles

MongoDB

  • ArchLinux: MongoDB
  • MongoDB documentation
      $ sudo pacman -S mongodb{,-tools}
      $ sudo systemctl start mongodb.service
      $ sudo systemctl enable mongodb.service
    

    And the MongoDB shell is immediately available, warning us that no access control is set up :

      $ mongo
    
      > use Scrapper
      switched to db Scrapper
    
      > db.Pages.insertOne({url:'http://fabic.net', label: 'fabicNet'})
      { "acknowledged" : true,
        "insertedId" : ObjectId("5ab9cf8a261bbd2cce0c71ea") }
    

Setup auth/nz

» Enable authentication (manual)

> use admin
switched to db admin

> db.createUser({ user: 'root', pwd: 'XXXXXX', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

Successfully added user: {
  "user" : "root",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
        "db" : "admin"
    }
  ]
}

But that is not enough, we’ll need to enable authentication :

$ mongo -u root -p admin

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
2018-03-27T09:14:12.528+0400 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed
> db.auth('root', 'mathinfo')

Error: Authentication failed.

MongoDB configuration options

$ sudo journalctl -b -f
$ tail -f /var/log/mongodb/mongod.log

$ sudo systemctl stop mongodb.service

$ sudo vim /etc/mongodb.conf

## EDIT:  ##
cat >> /etc/mongodb.conf <<EOF
# /etc/mongodb.conf
# Created on $(date), by $(whoami)

systemLog:
   quiet: false
   #destination: file
   #path: "/var/log/mongodb/mongod.log"
   #logAppend: true
   destination: syslog

storage:
   dbPath: /var/lib/mongodb
   journal:
      enabled: true

net:
   bindIp: 127.0.0.1
   port: 27017

setParameter:
   enableLocalhostAuthBypass: false

security:
   authorization: enabled
EOF

$ sudo systemctl start mongodb.service

So, strangely, mongo -u root -p won’t authenticate users unless the --authenticationDatabase admin argument is passed from the command line :

$ mongo -u root -p --authenticationDatabase admin

Or using db.auth() :

$ mongo

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3

> use admin
switched to db admin

> db.auth('root', 'mathinfo')
1

Create first normal user

Security > Auth/N > Users > Add Users

> use fabi
switched to db fabi

> db.createUser({ user: 'fabi', pwd: 'haiku', roles: [ {role: 'dbOwner', db: 'fabi'} ] })
Successfully added user: {
  "user" : "fabi",
  "roles" : [ { "role" : "dbOwner", "db" : "fabi" } ]
}
$ mongo
> use fabi
switched to db fabi

> db.auth('fabi', 'haiku')
1

Node.js

$ npm install mongodb bson-ext

Random

  • About npm’s npx ?
  • $(npm bin) typically evaluates to ./node_modules/.bin, ex.: $(npm bin)/cypress open

Pointers

EOF