node
and npm
Node.jsยฎ is a platform
built on Chrome's JavaScript runtime
for easily building fast, scalable network applications.
Node.js uses an event-driven, non-blocking I/O model
that makes it lightweight and efficient, perfect
for data-intensive real-time applications
that run across distributed devices.
from: https://www.dropbox.com/s/o9g4m7tug3yt1xx/jsconf2009-nodejs.pdf?dl=0
var result = db.query("select * from T");
// use result
db.query("select * from T",
function(result) {
// use result
}
);
from blog.udemy.com/learn-node-js/
const path = require('path')
package.json
"type": "module"
import path from path
socket.io
์งง์ ์ค์ ์ฝ๋๋ก ์น์ฑํ ๊ตฌํ ๊ฐ๋ฅ
package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.14.0",
"socket.io": "^4.1.2"
}
}
server.js
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('chat message', function (msg) {
io.emit('chat message', msg);
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});
index.html
<!doctype html>
<html lang="ko">
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var messages = document.getElementById('messages');
var form = document.getElementById('form');
var input = document.getElementById('input');
form.addEventListener('submit', function (e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
socket.on('chat message', function (msg) {
var item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
</script>
</body>
</html>
npm i
node server.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
npm init
npm install express --save
exp.js
filevar express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
node exp.js
npm install express-generator -g
express myweb
cd myweb && npm install
npm start
node bin/www
DEBUG=myweb:* node bin/www
app.METHOD(PATH, HANDLER)
app.get('/', function (req, res) {
res.send('Hello World!')
})
curl 'localhost:3000'
curl -XGET 'localhost:3000'
app.post('/', function (req, res) {
res.send('Got a POST request')
})
curl -XPOST 'localhost:3000'
app.put('/user', function (req, res) {
res.send('Got a PUT request at /user')
})
curl -XPUT 'localhost:3000/user'
app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})
curl -XDELETE 'localhost:3000/user'
get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, connect.
app.all('/secret', function (req, res, next) {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params)
})
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
node debug myscript.js
x = 5;
setTimeout(() => {
debugger;
console.log('world');
}, 1000);
console.log('hello');
npm install -g node-inspector
node-debug app.js
QUnit
Testing Essentials
UI ํ ์คํธ
$ cd ~/dev/mongodb/bin
$ mkdir -p ~/data/db
$ ./mongod --dbpath=/Users/kenu/data/db
$ cd ~/dev/mongodb/bin
$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use uptime
switched to db uptime
> db.addUser('uptime', 'okpass');
{
"user" : "uptime",
"readOnly" : false,
"pwd" : "fdc9e10c8f90fac0c9fe786f28cc04f4",
"_id" : ObjectId("5577b97a62555c0332f63e6f")
}
> exit
$ git clone git://github.com/fzaninotto/uptime.git
$ cd uptime
$ npm install
PM2
pm2-dev
: watch and restartnode server monitor