Dexter Jin

Update Experiment09

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('/books/:bookId', function (req, res) {
12 + var bookId = req.params.bookId;
13 + console.log(books[bookId]);
14 + res.send(books[bookId]);
15 +});
16 +
17 +/*
18 + * json representation of book object
19 +{
20 + "id" : 2,
21 + "name" : "book2",
22 + "price" : 2000,
23 + "author" : "jin"
24 +}
25 +*/
26 +app.post('/books', function (req, res) {
27 + // Create book information
28 + books[req.body.id] = [req.body.id, req.body.name, req.body.price, req.body.author];
29 + res.send(books[req.body.id]);
30 +})
31 +
32 +app.put('/books', function (req, res) {
33 + // Update book information
34 +
35 +})
36 +
37 +
38 +app.delete('/books/:bookId', function (req, res) {
39 + // Delete book information
40 +
41 +})
42 +var server = app.listen(80);
43 + 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 +
13 +app.get('/login', function (req, res) {
14 + // users 배열에서 찾도록 처리 해야 함
15 + // admin 여부를 확인하여 체크
16 + // req.body.id : ID
17 + // req.body.name : 패스워드
18 +
19 + res.send("Login");
20 +});
21 +
22 +app.get('/logout', function (req, res) {
23 + // Logout
24 + // 세션 유효 여부를 체크하고 세션 Delete
25 + req.session.userId = null;
26 + res.send("LogOut");
27 +
28 +});
29 +
30 +var auth = function (req, res, next) {
31 + // Session Check
32 + // 어드민 여부 체크 필요
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 +}