Dexter Jin

Rename

Showing 38 changed files with 0 additions and 202 deletions
1 -// mymodule.js에 정의된 모듈로 불러서 사용하도록 처리
1 -// 아래 2개의 함수를 모듈로 정의하는 것이 필요함
2 -function add(a,b) {
3 - return a+b;
4 -}
5 -function sub(a,b) {
6 - return a-b;
7 -}
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();
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 -// inherits를 활용하여 Producer construct가 EventEmitter Construct를 상속받도록 해야 함
12 -inherits(Producer, EventEmitter);
13 -
14 -// producer의 start 함수를 재정의 해야 함
15 -Producer.prototype.start = function () {
16 -
17 - // Random 스트링을 생성하여 이것을 Event에 실어서 보내야 하는 코드 작성
18 -
19 -
20 -}
21 -
22 -
23 -module.exports = Producer;
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 - var bookId = req.params.bookId;
13 - console.log(books[bookId]);
14 - res.send(books[bookId]);
15 -});
16 -
17 -/*
18 -{
19 - "id" : 2,
20 - "name" : "book2",
21 - "price" : 2000,
22 - "author" : "jin"
23 -}
24 -*/
25 -app.post('/book', function (req, res) {
26 - // Create book information
27 - books[req.body.id] = [req.body.id, req.body.name, req.body.price, req.body.author];
28 - res.send(books[req.body.id]);
29 -})
30 -
31 -app.put('/book/:bookId', function (req, res) {
32 - // Update book information
33 -
34 -})
35 -
36 -
37 -app.delete('/book/:bookId', function (req, res) {
38 - // Delete book information
39 -
40 -})
41 -var server = app.listen(80);
42 - 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 - // users 배열에서 찾도록 수정해야 함
17 - // admin 여부를 지원할 수 있도록 수정해야 함
18 - if (req.params.userId == userId && req.params.password == userPass) {
19 - req.session.userId = userId;
20 - }
21 - res.send("Login");
22 -});
23 -
24 -app.get('/logout/:userId', function (req, res) {
25 - // Logout
26 - req.session.userId = null;
27 - res.send("LogOut");
28 -
29 -});
30 -
31 -var auth = function (req, res, next) {
32 - // Session Check
33 - if (req.session.userId != null)
34 - next();
35 - else
36 - res.send("Error");
37 -
38 -};
39 -app.get('/user/:userId', auth,function (req, res) {
40 - // get User Information
41 - res.send("OK");
42 -});
43 -
44 -// 사용자 추가 시에 admin 여부도 추가해야 함
45 -
46 -var server = app.listen(80);
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 -}