Dexter Jin

Update Experiment09

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('/books/:bookId', function (req, res) {
var bookId = req.params.bookId;
console.log(books[bookId]);
res.send(books[bookId]);
});
/*
* json representation of book object
{
"id" : 2,
"name" : "book2",
"price" : 2000,
"author" : "jin"
}
*/
app.post('/books', 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('/books', function (req, res) {
// Update book information
})
app.delete('/books/: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();
app.get('/login', function (req, res) {
// users 배열에서 찾도록 처리 해야 함
// admin 여부를 확인하여 체크
// req.body.id : ID
// req.body.name : 패스워드
res.send("Login");
});
app.get('/logout', function (req, res) {
// Logout
// 세션 유효 여부를 체크하고 세션 Delete
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"
}
}