오석진

transport

let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let session = require('express-session')
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
let users = new Array();
users[0] = {
"userId" : 0,
"name" : "jin",
"password" : "abc",
"isAdmin" : true
}
app.put('/login', (req, res) => {
if(req.body.userId==users[req.body.userId].userId && req.body.password==users[req.body.userId].password){
req.session.userId=users[req.body.userId].isAdmin
res.send("Login");
}
else
res.send("invalid id")
// users 배열에서 찾도록 처리 해야 함
// admin 여부를 확인하여 체크
// req.body.id : ID
// req.body.password : 패스워드
});
app.put('/logout', (req, res) => {
// Logout
// 세션 유효 여부를 체크하고 세션 Delete
req.session.userId = null;
res.send("LogOut");
});
let auth = (req, res, next) => {
// Session Check
// 어드민 여부 체크 필요
if (req.session.userId != null && req.session.isAdmin==true)
next();
else
res.send("Error");
};
app.get('/user/:userId', auth, (req, res) => {
const userId=req.params.userId
console.log(users[userId]);
res.send(users[userId]);
// get User Information
// res.send("OK");
req.session.userId=true;
});
app.post('/user/:userId', auth, (req, res) => {
const userId=req.params.userId
users[userId]= {
"userId" : req.body.userId,
"name" : req.body.name,
"password" : req.body.password,
"isAdmin" : req.body.isAdmin
}
// users[userId]=[req.body.userId, req.body.name, req.body.password, req.body.isAdmin];
// get User Information
req.session.userId=true;
res.send("OK");
});
app.put('/user/:userId', auth, (req, res) => {
const userId=req.params.userId;
users[userId]= {
"userId" : req.body.userId,
"name" : req.body.name,
"password" : req.body.password,
"isAdmin" : req.body.isAdmin
}
// users[userId]=[req.body.userId, req.body.name, req.body.password, req.body.isAdmin];
// get User Information
req.session.userId=true;
res.send("OK");
});
app.delete('/user/:userId', auth, (req, res) => {
const userId=req.params.userId;
delete users[userId];
console.log(users);
req.session.userId=true;
// get User Information
res.send("OK");
});
// 사용자 추가 시에 admin 여부도 추가해야 함
let server = app.listen(80);
\ No newline at end of file
{
"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.18.1",
"express-session": "^1.15.2"
}
}