진성욱

experiments08 & experiments09 updated

Showing 39 changed files with 513 additions and 0 deletions
function add(a,b) {
return a+b;
}
function sub(a,b) {
return a-b;
}
var Producer = require('./producer');
var producer = new Producer();
producer.on('add', function(t) {
console.log('add:', t);
});
producer.start();
// stop the clock 10 seconds after
// setTimeout(function() {
// clock.stop();
// }, 10e3)
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var Producer = function () {
if (! (this instanceof Producer)) return new Producer();
EventEmitter.call(this);
}
module.exports = Producer;
var fs = require("fs");
var searchString = "test";
var events = require('events');
var eventEmitter = new events.EventEmitter();
var connectHandler = function connected() {
console.log('connection successful.');
eventEmitter.emit('data_received');
}
eventEmitter.on('connection', connectHandler);
eventEmitter.on('data_received', function(){
console.log('data received successfully.');
});
eventEmitter.emit('connection');
console.log("Program Ended.");
var events = require('events');
var eventEmitter = new events.EventEmitter();
var listner1 = function listner1() {
console.log('listner1 executed.');
}
var listner2 = function listner2() {
console.log('listner2 executed.');
}
eventEmitter.addListener('connection', listner1);
eventEmitter.on('connection', listner2);
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
eventEmitter.emit('connection');
eventEmitter.removeListener('connection', listner1);
console.log("Listner1 will not listen now.");
eventEmitter.emit('connection');
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
console.log("Program Ended.");
var dt = require('./mymodule');
console.log(dt.myDateTime());
exports.myDateTime = function () {
return Date();
};
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var books = new Array();
app.get('/book/:bookId', function (req, res) {
// Get book information
});
app.put('/book/:bookId', function (req, res) {
// Update book information
})
app.post('/book', function (req, res) {
// Create book information
})
app.delete('/book/:bookId', function (req, res) {
// Delete book information
})
var server = app.listen(23023);
console.log(books);
{
"name": "assignment01",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session')
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var users = new Array();
var userId = 'test';
var userPass = '1234';
app.get('/login/:userId/password/:password', function (req, res) {
if (req.params.userId == userId && req.params.password == userPass) {
req.session.userId = userId;
}
res.send("Login");
});
app.get('/logout/:userId', function (req, res) {
// Logout
req.session.userId = null;
res.send("LogOut");
});
var auth = function (req, res, next) {
// Session Check
if (req.session.userId != null)
next();
else
res.send("Error");
};
app.get('/user/:userId', auth,function (req, res) {
// get User Information
res.send("OK");
});
var server = app.listen(23023);
{
"name": "assignment02",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"express-session": "^1.15.2"
}
}
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('hello world');
})
app.route('/book')
.get(function (req, res) {
res.send('Get a random book');
})
.post(function (req, res) {
res.send('Add a book');
})
.put(function (req, res) {
res.send('Update the book');
});
var server = app.listen(23023);
{
"name": "approute",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
var birds = require('./birds');
app.get('/', function (req, res) {
res.send('hello world');
})
app.use('/birds',birds);
var server = app.listen(23023);
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.send('Birds home page');
})
router.get('/about', function (req, res) {
res.send('About birds');
});
module.exports=router;
{
"name": "expressrouter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(23023, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
{
"name": "helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
app.get('/b', function (req, res, next) {
console.log('the response will be sent by the next function ...')
next()
}, function (req, res) {
res.send('Hello from B!')
})
var cb0 = function (req, res, next) {
console.log("call by cb0");
next()
}
var cb1 = function (req, res, next) {
console.log("call by cb1");
res.send('Hello from C!')
}
app.get('/c', [cb0, cb1])
var server = app.listen(23023);
{
"name": "multiplehandler",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');
app.get('/', function(req, res) {
res.render('index', { title: 'OSS Page' });
});
var server = app.listen(23023);
{
"name": "render",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.5.6",
"express": "^4.15.2"
}
}
html>
<body>
<%= title %>
</body>
</html>
var express = require('express');
var app = express();
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params);
})
var server = app.listen(23023);
{
"name": "routeparameter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
// The routing path matches requests to /about
app.get('/about', function (req, res) {
res.send('about')
})
// The routing path matches requests to /random.text
app.get('/random.text', function (req, res) {
res.send('random.text')
})
// This route path matches abcd, abxcd, abRANDOMcd, ab123cd, and so on.
app.get('/ab*cd', function (req, res) {
res.send('ab*cd')
})
var server = app.listen(23023);
{
"name": "routingpath",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
var session = require('express-session')
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.get('/', function(req, res, next) {
var sess = req.session;
console.log(req.session);
if (sess.views) {
sess.views++;
res.send("session Views " + sess.views);
res.send();
} else {
req.session.views = 1;
res.send("welcome to the session demo. refresh!");
res.end();
}
});
var server = app.listen(23023);
{
"name": "session",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2",
"express-session": "^1.15.2"
}
}
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('hello world');
})
var server = app.listen(23023);
{
"name": "simplerouting",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
app.all('/', function (req, res, next) {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
app.get('/', function (req, res) {
res.send('hello world');
})
app.post('/', function (req, res) {
res.send('POST request to the homepage')
})
var server = app.listen(23023);
{
"name": "specialrouting",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(23023);
{
"name": "staticfiles",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
<html>
<body>
Public index.html
</body>
</html>