진성욱

experiments08 & experiments09 updated

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