Start / Terminate
Start / Terminate
// Starting
node app_name.js
// Terminating
process.exit(1)
Environment
Environment
process.env.NODE_ENV
// developmnet
process.env.NODE_ENV = "production"
Arguments
Arguments
// Passing arguments
node app.js arg_val
(or)
node app.js arg_name=arg_val
// Accessing arguments
const args = process.argv.slice(2)
args[0]
(or)
const args = require('minimist')(process.argv.slice(2))
args['name']
(or)
const args = process.argv.slice(2)
(or)
process.argv.forEach((val, index) => {
console.log(`${index}: ${val}`)
})
Export / Import
Export / Import
// Type 1
// Exporting
const obj = {
name_1 : 'val_1',
name_2 : 'val_3'
}
module.exports = obj
// Importing
const obj = require('./obj')
// Type 2
// Exporting
const obj = {
name_1 : 'val_1',
name_2 : 'val_3'
}
exports.obj = obj
// Importing
const items = require('./obj')
items.obj
// Type 3
// Exporting
exports.obj = {
name_1 : 'val_1',
name_2 : 'val_3'
}
// Importing
const car = require('./items').obj
package.json
package.json
// sample package.json
{
"name": "test-project",
"version": "1.0.0",
"author": {
"name": "some_name",
"email": "some_name@whatever.com",
"url": "https://whatever.com"
},
"homepage": "https://whatever.com/package",
"description": "A package to work with strings",
"keywords": ["email", "machine learning", "ai"],
"contributors": ["some_name (https://whatever.com)"],
"license": "MIT",
"main": "src/main.js",
"private": true,
"bugs": "https://github.com/whatever/package/issues",
"repository": "github:whatever/testing",
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"unit": "jest --config test/unit/jest.conf.js --coverage",
"test": "npm run unit",
"lint": "eslint --ext .js,.vue src test/unit",
"build": "node build/build.js"
},
"dependencies": {
"vue": "^2.5.2"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1"
},
"engines": {
"node": ">= 6.0.0",
"npm": ">= 3.0.0",
"yarn": "^0.13.0"
},
"browserslist": ["> 1%", "last 2 versions", "not ie <= 8"]
}
* version - indicates the current version
* name - sets the application/package name
* description - is a brief description of the app/package
* main - set the entry point for the application
* private - if set to true prevents the app/package to be accidentally published on npm
* scripts - defines a set of node scripts you can run
* dependencies - sets a list of npm packages installed as dependencies
* devDependencies - sets a list of npm packages installed as development dependencies
* engines - sets which versions of Node.js this package/app works on
* browserslist - is used to tell which browsers (and their versions) you want to support
* ^: It will only do updates that do not change the leftmost non-zero number.
If you write ^0.13.0, when running npm update, it can update to 0.13.1, 0.13.2,
and so on, but not to 0.14.0 or above. If you write ^1.13.0, when running npm update,
it can update to 1.13.1, 1.14.0 and so on, but will not update to 2.0.0 or above.
* ~: if you write ~0.13.0, when running npm update it can update to patch releases: 0.13.1 is ok,
but 0.14.0 is not.
* >: you accept any version higher than the one you specify
* >=: you accept any version equal to or higher than the one you specify
* <=: you accept any version equal or lower to the one you specify
* <: you accept any version lower to the one you specify
* =: you accept that exact version
* -: you accept a range of versions. Example: 2.1.0 - 2.6.2
* ||: you combine sets. Example: < 2.1 || > 2.6
Event loop
Event loop
// Event loop run for every iteration in the call stack
// nextTick run the code at the end of the current iteration
process.nextTick(() => {
// code
})
// setTimeout/setImmediate run the code as the next iteration
setTimeout(() => {
// code
}, 0)
(or)
setImmediate(() => {
// code
})
Event Emitter
Event Emitter
// Custom event emitters
const EventEmitter = require('events')
const custom_ev = new EventEmitter()
custom_ev.on('start', () => {
console.log('started')
})
custom_ev.emit('start')
(or)
custom_ev.on('start', number => {
console.log(`started ${number}`)
})
custom_ev.emit('start', 23)
(or)
custom_ev.on('start', (start, end) => {
console.log(`started from ${start} to ${end}`)
})
custom_ev.emit('start', 1, 100)
Http server
Http server
// Building server
const http = require('http')
const port = process.env.PORT
const server = http.createServer(function (req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.end('Hello, World!')
})
server.listen(port, function () {
console.log(`Server running at port ${port}`)
})
Http request
Http request
// GET
const https = require('https')
const options = {
hostname: 'whatever.com',
port: 443,
path: '/todos',
method: 'GET'
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.end()
// POST
// PUT
// DELETE
const https = require('https')
const data = JSON.stringify({
todo: 'Buy the milk'
})
const options = {
hostname: 'whatever.com',
port: 443,
path: '/todos',
method: 'POST',
(or)
method: 'PUT',
(or)
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()