Node Flashcards
initialize N project with standard defaults
npm init -y
initialize N project step by step
npm init
install W for a N server
npm i -D webpack webpack-cli _webpack-dev-server
run W in specific mode
webpack –mode=«mode»
» mode: none, developmen, production
name of standard W config file
webpack.config.js
minimum W config
» 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
use current work directory path in N
import path from ‘path’
path.resolve(__dirname, ‘«relative path»’)
set path for public/asset and output bundle folder in W config
» output: {
publicPath: «path»
}
» path: relative to server root: ‘/«folder»/’
» path: relative to index.html: ‘«folder»/’, ‘../«folder»/’
» from index.html to output script
set serving root of development server in W config
» devServer: {
contentBase: «server root»
}
run W development server
webpack-dev-server
setup loader in W
» npm i -D «_package» «package loader» » module > rules: [{ test: «test regex», use: ['«loader2»', '«loader1»'] }]
» right loader is first
show W dev server error in browser window
» devServer: {
overlay: true
}
setup CSS in W
» npm i -D css-loader style-loader » module > rules: [{ test: /\.css$/, use: ['style-loader', 'css-loader'] }]
setup HTML in W
» 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 ] }]
setup image load in W
» module > rules: [{ test: /\.(jpg|jpeg|png|gif)$/, use: { loader: 'file-loader', options: { name: '«images folder»/[name].[ext]' } } }]
» configured html-loader required
name of Babel configuration file
.babelrc
» is JSON file
setup Babel in W
» 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/ }]
setup minimal Express server in N
import express from ‘express’
const app = express()
app.listen(«port», () => {
console.log(Server is listening on port {«port»}
)
})
setup SASS in W
» npm i -D sass(/node-sass) sass-loader » module > rules: [{ test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }]
setup TypeScript in W
» 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/ }]
setup plugin in W
» npm i -D «plugin» » const «plugin» = require('«plugin»'); » plugins: [ new «plugin»() ]
setup code splitting in W
» optimization > splitChunks: {
chunks: ‘all’,
_minSize: 0
}
» reduces duplicate code and libraries
setup webpack-dev-server with hot reloading in W
» npm i -D webpack-dev-server » const webpack = require('webpack') » devServer: { hot: true } » plugins: [ new webpack.HotModuleReplacementPlugin() ]
setup source mapping to original code for webpack-dev-server
» devtool: ‘(inline-)source-map’
» inline-: works in production
ES6 import of element from other file in N
import «default element alias» from ‘./«file path»’
import * as «alias» from ‘./«file path»’
import _«default element alias», { «element1», «_element2», _… } from ‘./«file path»’
access file system in N
import fs from ‘fs’
ES6 export of element from file in N
export _default «element»
export {«element» _as default, «_other element», _… }
install specific version of N package
npm i «package»@«version»
setup hot reloading on save of N
» npm i -D nodemon
» package.json > scripts:
nodemon «entry point»
get terminal arguments in N
process.argv
» is JS array
transform buffer to string in N
«buffer».toString()
debug N in VSC and Chrome
» put «debugger» in code
» run: node inspect «entry point»
» restart application with «restart» in terminal
compare dart-sass to node-sass
» dart gets new features earliest
» dart > node on cross platform
» dart installs faster than node
» dart runs slower than node
setup static assets folder in Express
app.use(express.static(«public folder path»))
set server setting in Express
app.set(«key», «value»)
create a mongodb client in N
import { MongoClient } from ‘mongodb’
MongoClient.connect(«url», «_options», (error, client) => {
client…
})
connect to a database with a mongodb client in N
const db = client.db(«database name»)
setup parsing of incoming json in Express
app.use(express.json())
access request parameters in Express
app.«http method»(‘«url»/:«param1»’, (req, res) => {
req.params.«param1»…
})
setup get route in Express
app.get(‘«url»’, (req, res) => {
…
})
setup post route in Express
app.post(‘«url»’, (req, res) => {
…
})
setup router in Express
const router = express.Router()
router. «route handler1»
router. «route handler2»
app.use(‘/«pre route»’, router)
» route handler: «http method»(‘«url»’, (req, res) => { … })
add middleware to Express server
app.use(«middleware»)
add middleware to Express route
app.«http method»( '«url»', «middleware», (req, res) => { ... } )
access query parameters in Express
app.«http method»(‘«url»’, (req, res) => {
req.query…
})
setup error handling for route in Express
app.«http method»('«url»', (req, res) => { // handle success }, (err, req, res, next) => { // handle error }