Dexter Jin

Rename

Showing 38 changed files with 0 additions and 202 deletions
// mymodule.js에 정의된 모듈로 불러서 사용하도록 처리
// 아래 2개의 함수를 모듈로 정의하는 것이 필요함
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();
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var Producer = function () {
if (! (this instanceof Producer)) return new Producer();
EventEmitter.call(this);
}
// inherits를 활용하여 Producer construct가 EventEmitter Construct를 상속받도록 해야 함
inherits(Producer, EventEmitter);
// producer의 start 함수를 재정의 해야 함
Producer.prototype.start = function () {
// Random 스트링을 생성하여 이것을 Event에 실어서 보내야 하는 코드 작성
}
module.exports = Producer;
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) {
var bookId = req.params.bookId;
console.log(books[bookId]);
res.send(books[bookId]);
});
/*
{
"id" : 2,
"name" : "book2",
"price" : 2000,
"author" : "jin"
}
*/
app.post('/book', function (req, res) {
// Create book information
books[req.body.id] = [req.body.id, req.body.name, req.body.price, req.body.author];
res.send(books[req.body.id]);
})
app.put('/book/:bookId', function (req, res) {
// Update book information
})
app.delete('/book/:bookId', function (req, res) {
// Delete book information
})
var server = app.listen(80);
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) {
// users 배열에서 찾도록 수정해야 함
// admin 여부를 지원할 수 있도록 수정해야 함
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");
});
// 사용자 추가 시에 admin 여부도 추가해야 함
var server = app.listen(80);
{
"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"
}
}