Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js crash course #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
20 changes: 0 additions & 20 deletions .description

This file was deleted.

107 changes: 107 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#DS_Store
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
Binary file added Reference/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions Reference/event_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const EventEmitter = require('events')

// Create class
class MyEmitter extends EventEmitter { }

// Init object
const myEmitter = new MyEmitter()

// Event listener
myEmitter.on('event', () => console.log('Event Fired!'))

// Init event
myEmitter.emit('event')
myEmitter.emit('event')
myEmitter.emit('event')
myEmitter.emit('event')


9 changes: 9 additions & 0 deletions Reference/fs_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs')
const path = require('path')

// Rename file
fs.rename(path.join(__dirname, '/test', 'hello.txt'), path.join(__dirname, '/test', 'helloworld.txt'), function(err){
if(err) throw err
console.log('File renamed...')
})
// File renamed...
10 changes: 10 additions & 0 deletions Reference/http_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const http = require('http')

// Create server object
http
.createServer((req, res) => {
// Write response
res.write('Hello World')
res.end()
})
.listen(5000, () => console.log('Server running...'))
22 changes: 22 additions & 0 deletions Reference/os_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const os = require('os')

// Pladform
console.log(os.platform())

// CPU Arch
console.log(os.arch())

// CPU Core Info
console.log(os.cpus())

// Free memory
console.log(os.freemem())

// Total memory
console.log(os.totalmem())

// Home Dir
console.log(os.homedir())

// Uptime
console.log(os.uptime())
27 changes: 27 additions & 0 deletions Reference/path_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const path = require('path')

// Base file name
console.log(path.basename(__filename))
//path_demo.js

// Directory name
console.log(path.dirname(__filename))
///Users/matthijsblauw/Documents/1. School/Blok 1/Sprint 9/the-web-is-for-everyone-coding-the-curbs/Reference

// File extention
console.log(path.extname(__filename))

// Create path object
console.log(path.parse(__filename))
/*{
root: '/',
dir: '/Users/matthijsblauw/Documents/1. School/Blok 1/Sprint 9/the-web-is-for-everyone-coding-the-curbs/Reference',
base: 'path_demo.js',
ext: '.js',
name: 'path_demo'
} */

// Concatenate paths
console.log(path.join(__dirname, 'test', 'hello.html'))
// ../the-web-is-for-everyone-coding-the-curbs/Reference/test/hello.html

1 change: 1 addition & 0 deletions Reference/test/helloworld.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hallo World! Ik hou van Node.js
38 changes: 38 additions & 0 deletions Reference/url_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const url = require('url')

const myUrl = new URL('http://mybsite.com/helo.html?id=100&status=active')

// Serialize URL
console.log(myUrl.href)
console.log(myUrl.toString())
// http://mybsite.com/helo.html?id=100&status=active

// Host (root domain)
console.log(myUrl.host)
// Hostname (does net get port)
console.log(myUrl.hostname)
// mybsite.com

// Pathname
console.log(myUrl.pathname)
// /helo.html

// Serialized query
console.log(myUrl.search)
// ?id=100&status=active

// Params object
console.log(myUrl.searchParams)
// URLSearchParams { 'id' => '100', 'status' => 'active' }

// Add param
myUrl.searchParams.append('abc', '123')
console.log(myUrl.searchParams)
// URLSearchParams { 'id' => '100', 'status' => 'active', 'abc' => '123' }

// Loop through params
myUrl.searchParams.forEach((value, name) => console.log(`${name}: ${value}`))
// id: 100
// status: active
// abc: 123

Empty file removed assets/.gitkeep
Empty file.
74 changes: 0 additions & 74 deletions docs/INSTRUCTIONS.md

This file was deleted.

36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const http = require('http')
const path = require('path')
const fs = require('fs')

const server = http.createServer((req, res) => {
console.log(req.url)
if(req.url === '/'){
fs.readFile(path.join(__dirname, 'public', 'index.html'), (err, content) => {
if(err) throw err
res.writeHead(200, { 'Content-Type': 'text/html'})
res.end(content)
})
}

if(req.url === '/about'){
fs.readFile(path.join(__dirname, 'public', 'about.html'), (err, content) => {
if(err) throw err
res.writeHead(200, { 'Content-Type': 'text/html'})
res.end(content)
})
}

// Rest API of een micro service
if(req.url === '/api/users'){
const users = [
{ name: 'Matthijs Blauw', age: '21'},
{ name: 'Emilio Rosini', age: '20'}
]
res.writeHead(200, { 'Content-Type': 'application/json'})
res.end(JSON.stringify(users))
}
})

const PORT = process.env.PORT || 5000

server.listen(PORT, () => console.log(`Server running on port ${PORT}`))
20 changes: 20 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const EventEmitter = require('events')
const uuid = require('uuid')

class Logger extends EventEmitter{
log(msg) {
// Class event
this.emit('message', { id: uuid.v4(), msg })
}
}

// module.exports = Logger
// const Logger = require('./logger')

const logger = new Logger()

logger.on('message', (data) => console.log('Called Listener', data))

logger.log('Hello World')
logger.log('Hi')
logger.log('Hello')
Loading