Node Flashcards

1
Q

initialize N project with standard defaults

A

npm init -y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

initialize N project step by step

A

npm init

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

install W for a N server

A

npm i -D webpack webpack-cli _webpack-dev-server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

run W in specific mode

A

webpack –mode=«mode»

» mode: none, developmen, production

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

name of standard W config file

A

webpack.config.js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

minimum W config

A
» webpack.config.js:
module.exports = {
  entry: '«entry filename/path»',
  mode: '«mode»',
  output: {
    filename: «output filename»,
    _path: «absolute output path»,
    _publicPath: «relative public path»
  }
}

» !module.exports
» mode: development, production, none
» path: path.resolve(__dirname, ‘«folder»’)
» dev server: needs both output AND public path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

use current work directory path in N

A

import path from ‘path’

path.resolve(__dirname, ‘«relative path»’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

set path for public/asset and output bundle folder in W config

A

» output: {
publicPath: «path»
}

» path: relative to server root: ‘/«folder»/’
» path: relative to index.html: ‘«folder»/’, ‘../«folder»/’
» from index.html to output script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

set serving root of development server in W config

A

» devServer: {
contentBase: «server root»
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

run W development server

A

webpack-dev-server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

setup loader in W

A
» npm i -D «_package» «package loader»
» module > rules: [{
  test: «test regex»,
  use: ['«loader2»', '«loader1»']
}]

» right loader is first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

show W dev server error in browser window

A

» devServer: {
overlay: true
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

setup CSS in W

A
» npm i -D css-loader style-loader
» module > rules: [{
  test: /\.css$/,
  use: ['style-loader', 'css-loader']
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

setup HTML in W

A
» npm i -D html-loader extract-loader file-loader
» module > rules: [{
  test: /\.html$/,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].html'
      }
    },   // resolves import/require
    'extract-loader',   // resolves urls
    'html-loader'   // loads raw html
  ]
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

setup image load in W

A
» module > rules: [{
  test: /\.(jpg|jpeg|png|gif)$/,
  use: {
    loader: 'file-loader',
    options: {
      name: '«images folder»/[name].[ext]'
    }
  }
}]

» configured html-loader required

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

name of Babel configuration file

A

.babelrc

» is JSON file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

setup Babel in W

A
» npm i -D @babel/core @babel/preset-env babel-loader
» module > rules: [{
  test: /\.js$/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  },
  exclude: /node_modules/
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

setup minimal Express server in N

A

import express from ‘express’

const app = express()

app.listen(«port», () => {
console.log(Server is listening on port {«port»})
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

setup SASS in W

A
» npm i -D sass(/node-sass) sass-loader
» module > rules: [{
  test: /\.scss$/,
  use: ['style-loader', 'css-loader', 'sass-loader']
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

setup TypeScript in W

A
» npm i -D typescript ts-loader
» entry: './src/index.ts'
» resolve > extensions: [ _'.tsx', '.ts', '.js' ]
» module > rules: [{
  test: /\.tsx?$/,
  use: 'ts-loader',
  exclude: /node_modules/
}]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

setup plugin in W

A
» npm i -D «plugin»
» const «plugin» = require('«plugin»');
» plugins: [
  new «plugin»()
]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

setup code splitting in W

A

» optimization > splitChunks: {
chunks: ‘all’,
_minSize: 0
}

» reduces duplicate code and libraries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

setup webpack-dev-server with hot reloading in W

A
» npm i -D webpack-dev-server
» const webpack = require('webpack')
» devServer: {
  hot: true
 }
» plugins: [
  new webpack.HotModuleReplacementPlugin()
]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

setup source mapping to original code for webpack-dev-server

A

» devtool: ‘(inline-)source-map’

» inline-: works in production

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

ES6 import of element from other file in N

A

import «default element alias» from ‘./«file path»’
import * as «alias» from ‘./«file path»’
import _«default element alias», { «element1», «_element2», _… } from ‘./«file path»’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

access file system in N

A

import fs from ‘fs’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

ES6 export of element from file in N

A

export _default «element»

export {«element» _as default, «_other element», _… }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

install specific version of N package

A

npm i «package»@«version»

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

setup hot reloading on save of N

A

» npm i -D nodemon
» package.json > scripts:
nodemon «entry point»

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

get terminal arguments in N

A

process.argv

» is JS array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

transform buffer to string in N

A

«buffer».toString()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

debug N in VSC and Chrome

A

» put «debugger» in code
» run: node inspect «entry point»

» restart application with «restart» in terminal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

compare dart-sass to node-sass

A

» dart gets new features earliest
» dart > node on cross platform
» dart installs faster than node
» dart runs slower than node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

setup static assets folder in Express

A

app.use(express.static(«public folder path»))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

set server setting in Express

A

app.set(«key», «value»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

create a mongodb client in N

A

import { MongoClient } from ‘mongodb’

MongoClient.connect(«url», «_options», (error, client) => {
client…
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

connect to a database with a mongodb client in N

A

const db = client.db(«database name»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

setup parsing of incoming json in Express

A

app.use(express.json())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

access request parameters in Express

A

app.«http method»(‘«url»/:«param1»’, (req, res) => {
req.params.«param1»…
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

setup get route in Express

A

app.get(‘«url»’, (req, res) => {

})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

setup post route in Express

A

app.post(‘«url»’, (req, res) => {

})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

setup router in Express

A

const router = express.Router()

router. «route handler1»
router. «route handler2»

app.use(‘/«pre route»’, router)

» route handler: «http method»(‘«url»’, (req, res) => { … })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

add middleware to Express server

A

app.use(«middleware»)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

add middleware to Express route

A
app.«http method»(
  '«url»',
  «middleware»,
  (req, res) => { ... }
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

access query parameters in Express

A

app.«http method»(‘«url»’, (req, res) => {
req.query…
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

setup error handling for route in Express

A
app.«http method»('«url»', (req, res) => {
  // handle success
}, (err, req, res, next) => {
  // handle error
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

uninstall package in N

A

npm r «package»

» also: remove, rm, un, uninstall, unlink

48
Q

setup Socket.io on an Express server in N

A

import http from ‘http’

import express from ‘express’
import socketio from ‘socket.io’

const app = express()
const server = http.createServer(app)
const io = socketio(server)

server.listen(«port», «callback»)

49
Q

listen for Socket.io connection in N

A

io.on(‘connection’, «callback»)

50
Q

setup Socket.io in the client in N

A
≤body≥
  ...
  ≤script src="/socket.io/socket.io.js"≥≤/script≥
  ≤script≥  
    io()
  ≤/script≥
≤body≥
51
Q

send Socket.io event to »the« connected client in N

A

socket.emit(«event», «payload»)

52
Q

listen for Socket.io events of a connection in N

A

socket.on(«event», «callback»)

53
Q

send Socket.io event from server to »all« connected clients in N

A

io.emit(«event», «payload»)

54
Q

send Socket.io event to »all« connected clients »except« the current ones in N

A

socket.broadcast.emit(«event», «payload»)

55
Q

listen for when a Socket.io connection ends in N

A

socket.on(‘disconnect’, «callback»)

56
Q

acknowledge event in Socket.io in N

A
» sender:
«io/socket».emit(«event», «payload», «ack callback»)
----------
» receiver:
«io/socket».on(«event», («payload», ack) => {
  ...
  ack()
}
57
Q

setup Babel script without Webpack in N

A
» npm i -D @babel/core @babel/preset-env
» .babelrc: {
  "presets": ["@babel/preset-env"]
}
» package.json > scripts:
"start": "babel-node «path to entry point»"
58
Q

setup GQL API server with graphql-yoga in N

A

import { GraphQLServer } from ‘graphql-yoga’

» import context: db, …
» import resolvers: Query, Mutation, custom types

const server = new GraphQLServer({
  typeDefs: '«path to schema»',
  resolvers: { «resolvers» },
  context: { «context» }
})

server.start(«_options», «callback»)

59
Q

what are the 5 scalar types in GQL?

A
» Boolean
» Int
» Float
» String
» ID
60
Q

setup nodemon script with Babel in N

A

» package.json > scripts:

nodemon «entry point» –exec babel-node

61
Q

setup plugin for Babel in N

A

» npm i -D «plugin»
» .babelrc: {
“plugins”: [“«plugin»”]
}

62
Q

set input or return value to required in GQL schema in N

A

» schema.graphql:

…«value»!

63
Q

what are the 4 GQL resolver arguments?

A

» parent: parent element
» args: client data
» ctx: universal elements like db, …
» info: query fileds/path, fragments, …

64
Q

query data from GQL API

A
query {
  «object to return» {
    «_fields to return»
  }
}
65
Q

setup create, update or delete operation in GQL schema in N

A
» schema.graphql:
type Mutation {
  «operation»(
    _id: ID!,
    «_payload label»: «payload type»!
  ): «return type»!
}
66
Q

create data with GQL API

A
mutation {
  «create function»(
    id: «id»,
    «payload label»: «payload»
  ) {
    «values to return»
  }
}
67
Q

setup input type in GQL schema in N

A
» schema.graphql:
input «input type» {
  «field1»: «type1»,
  ...
}
68
Q

setup create, update or delete operation in GQL resolver in N

A
» resolvers > Mutation.js:
const Mutation = {
  _async «operation»(parent, args, ctx, info) {
    // create, update or delete element
    return «element»
  }
}
69
Q

delete data with GQL API

A
mutation {
  «delete function»(id: «id») {
    «values to return»
  }
}
70
Q

update data with GQL API

A
mutation {
  «update function»(
    id: «id»,
    «payload label»: «payload»
  ) {
    «values to return»
  }
}
71
Q

setup read operation in GQL schema in N

A
» schema.graphql:
type Query {
  «element»(
    «_query arg1»: «arg type1»,
    «_query arg2»: «arg type2»
  ): «return type»!
}
72
Q

setup enum in GQL schema in N

A
» schema.graphql:
enum «name» {
  «value1»
  «value2»
  ...
}

» values are strings
» choose UPPERCASE values

73
Q

setup server for GQL subscriptions with graphql-yoga in N

A

» index.js:
import { GraphQLServer, PubSub } from ‘graphql-yoga’

const pubsub = new PubSub()

const server = new GraphQLServer({
  ...,
  context: {
    pubsub
  }
})

server.start(«_options», «callback»)

74
Q

setup subscription in GQL schema in N

A
» schema.graphql:
type Subscription {
  «subscription name»(
    «_subscription arg»: «arg type»
  ): «return type»!
}
75
Q

setup resolver for GQL subscription in N

A
» resolvers > Subscription.js:
const Subscription = {
  «subscription name»: {
    subscribe(parent, args, { pubsub }, info) {
      ...
      return pubsub.asyncIterator('«subscription channel»')
    }
  }
}
76
Q

publish to GQL subscription channel in N

A
» resolvers > Mutation.js:
const Mutation = {
  «mutation function»(parent, args, { pubsub }, info) {
    ...
    pubsub.publish('«subscription channel»', {
      «subscription name»: {
        «fields and payload»
      }
    })
  }
}
77
Q

create hash in N

A

import bcrypt from ‘bcryptjs’

const hash = await bcrypt.hash(«input», «salt»)

» recommended salt: 14

78
Q

create JWT in N

A

import jwt from ‘jsonwebtoken’

const token = jwt.sign(
  «input object»,
  «secret»,
  «_options»,
  «_callback»
)

» options: { expiresIn: ‘7d’, ‘10h’, ‘120’ } // ms
» options: { algorithm: ‘HS256’ } // is default
» callback: (err, token) => { … } // makes it async

79
Q

decode JWT token in N

A

import jwt from ‘jsonwebtoken’

const content = jwt.decode(«token»)

80
Q

verify JWT in N

A

import jwt from ‘jsonwebtoken’

const content = jwt.verify(
  «token»,
  «secret»,
  «_options»,
  «_callback»
)

» throws error if invalid
» options: { algorithms: [‘HS256’, ‘HS384’] }
» callback: (err, decoded) => { … } // makes it async

81
Q

check hash in N

A

import bcrypt from ‘bcryptjs’

await bcrypt.compare(«input», «hash»)

» true or false

82
Q

setup script using local environment variables in N

A

» npm i -D env-cmd
» package.json > scripts:
env-cmd _-f «_path to env file» «command»

» .env is default file

83
Q

setup Jest test suite in N

A
» npm i -D jest
» tests > «suite name».test.js:
test(«test name», () => {
  ...
  expect(«variable»).«assertion»_(«target value»)
})
84
Q

configure scripts to run before Jest test run in N

A
» package.json:
"jest": {
  "globalSetup": "«path to setup file»"
  "globalTeardown": "«path to teardown file»"
}
85
Q

setup start of db server before Jest test run in N

A

» global setup file:
_require(‘babel-register’)
_require(‘@babel/polyfill/noConflict’)

const server = «import db server»

module.exports = async () => {
global.«dbserver name» = await server.«start»( … )
}

» no ES6 syntax yet
» must be async function
» might need babel-register and polyfill

86
Q

setup stop of db server after Jest test run in N

A

» global teardown file:
_require(‘babel-register’)
_require(‘@babel/polyfill/noConflict’)

module.exports = async () => {
global.«dbservername».«stop»()
}

» no ES6 syntax yet
» must be async function
» might need babel-register and polyfill

87
Q

setup SASS to CSS compilation script in watch mode in N

A

» npm i -D node-sass
» package.json > scripts:
node-sass «input path» «output path» -w

88
Q

setup SASS to CSS compilation script in N

A

» npm i -D node-sass
» package.json > scripts:
node-sass «input» «output»

89
Q

setup CSS concatenation script in N

A

» npm i -D concat
» package.json > scripts:
concat -o «output» «input1» «input2» …

90
Q

setup CSS autoprefixing script in N

A

» npm i -D autoprefixer postcss-cli
» package.json > scripts:
postcss -u autoprefixer -b ‘«browserslist»’ «input» -o «output»

» browserlist: defaults, last 2 versions, …

91
Q

setup CSS compression script in N

A

» npm i -D node-sass
» package.json > scripts:
node-sass «input» «output» –output-style compressed

92
Q

setup N scripts to run sequentially

A

» npm i -D npm-run-all
» package.json > scripts:
npm-run-all «script1» «script2» …

93
Q

setup N scripts to run at the same time

A

» npm i -D npm-run-all
» package.json > scripts:
npm-run-all –parallel «script1» «script2» …

94
Q

write middleware in Express in N

A

const «middleware» = (req, res, next) => {

next()
}

95
Q

setup payload for query, mutation or subscription in GQL schema in N

A
» schema.graphql:
type «Query|Mutation|Subscription» {
  «element»(
    «payload label»: «payload type»
  ): «return type»
}
96
Q

access payload for query or mutation in GQL resolver in N

A
» resolvers > «Query|Mutation».js:
const «Query|Mutation» = {
  _async «element»(parent, args, ctx, info) {
    ...args.«arg1»
    ...
  }
}
97
Q

setup relationship in GQL schema in N

A

» schema.graphql:
type «custom type1» {
«field»: «custom type2»|[«custom type2»]
}

98
Q

setup relationship in GQL resolver in N

A
» resolvers > «custom type1».js:
const «custom type1» = {
  «field»(parent, args, ctx, info) {
    ...parent
    return «custom type2»|[«custom type2»]
  }
}
99
Q

setup custom type in GQL schema in N

A

» schema.graphql:
type «custom type» {
«field»: «type»
}

100
Q

setup nodemon script to watch specific file extensions

A

» package.json > scripts:

nodemon «entry point» –ext js,«extention1»,…

101
Q

use reusable field selection for GQL API

A
query/mutation/subscription {
  «object to return» {
    «_fields to return»
    ...«selection name»
  }
}
102
Q

setup Apollo client in N

A

import { ApolloClient, InMemoryCache } from ‘@apollo/client’;

const client = new ApolloClient({
  uri: '«host uri»',
  _link: «_link array»,
  cache: new InMemoryCache()
})
103
Q

run Jest test suite in N

A

jest _–watch

104
Q

query/mutate data with external variables from GQL API

A
query/mutation «operation name»(
  $«variable»: «var type», ...
){
  «object to return»(«arg name»: $«variable», ...) {
    «_fields to return»
  }
}
105
Q

write GQL query for Apollo client in N

A

import { gql } from ‘@apollo/client’

const query = gql «GQL query»

106
Q

execute GQL query in Apollo client in N

A

_await client.query({ query: «GQL query» })

107
Q

execute GQL query with variable(s) in Apollo client in N

A

_await client.query({
query: «GQL query»,
variables: { «var1»: «val1» }
})

108
Q

execute GQL mutation in Apollo client in N

A

_await client.mutate({ mutation: «GQL query» })

109
Q

execute GQL mutation with variable(s) in Apollo client in N

A

_await client.mutate({
mutation: «GQL query»,
variables: { «var1»: «val1» }
})

110
Q

execute GQL subscription in Apollo client in N

A

_await client.subscribe({ query: «GQL query» })

111
Q

non-ES6 import of element from other file in N

A

const «default element alias» = require(‘./«file path»’)

const «sub element alias» = require(‘./«file path»’).«sub element»

112
Q

non-ES6 export of element from file in N

A

module. exports = «element»

module. exports = { «element1», «element2» }

113
Q

transform buffer to json in N

A

«buffer».toJSON()

114
Q

define reusable field selection for GQL API

A
fragment «selection name» on «custom type» {
  «_field1»
  «_relation1» { }
  ...
}
115
Q

run W compilation

A

webpack

116
Q

run W compilation in watch mode

A

webpack –watch

117
Q

setup hot reloading for N backend server

A

» npm i -D clean-webpack-plugin webpack-node-externals
» webpack.config.json:
entry: entry: [‘webpack/hot/poll?1000’, «entry point»],
externals: [
nodeExternals({ allowlist: [‘webpack/hot/poll?1000’] })
],
plugins: [
new CleanWebpackPlugin(),
new webpack.HotModuleReplacementPlugin()
]

» node entry point:
if (module.hot) {
  module.hot.accept()
  module.hot.dispose(() => { /* stop server */ })
}