-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week17 #18
base: master
Are you sure you want to change the base?
Week17 #18
Conversation
homePage: async(req, res) => { | ||
const articles = await Articles.findAll({ | ||
order: [['id', 'DESC']], | ||
include: User | ||
}) | ||
res.render('index', { | ||
articles | ||
}) | ||
}, | ||
add: (req, res) => { | ||
const {userId} = req.session //這從登入帳號存在session的帳號 ,用解構語法拿出來的 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
縮排不一致
我在猜可能是 tab 和 space 的差別,
你可能需要調整一下你的編輯器或者 IDE。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cwc329 想請教助教,如果這時候用VSCODE自動排版快捷鍵,SHIFT ALT F 讓它自動排是好的縮排嗎?
因為排版問題其實有先調過了,但每次在寫新的一頁的時候又會跑掉,我都是按tab去寫,但有時候我的編輯器會跳2格為單位,有時候是四格,沒特別注意就會沒發現到又跳掉了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我現在用的 IDE 不是 vscode,所以不太知道要怎麼用。
不過想要一致的話,JS 可以考慮用 eslint。
這樣只要規則一樣然後讓 eslint 自己 format 就可以保證一致了。
而且主流的編輯器以及 IDE 幾乎都有 eslint 的 plugin 可以用。
title, | ||
content, | ||
UserId: userId | ||
}).then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以改用 async/await
}, | ||
|
||
articlePage: async(req, res) => { | ||
// id 本來就是 URL 的一部分,所以不用特地檢查 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
任何使用者傳進來的資訊都要檢查。
|
||
Articles.findOne({ | ||
where: { | ||
id: req.params.id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面已經解構了這邊可以直接用變數。
|
||
|
||
app.use(session({ | ||
secret: 'keyboard cat', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secret 不要用 hard code,
使用環境變數。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cwc329 抱歉助教,這邊的寫法是直接抄網路的方式,所以有點看不懂您提醒的意思,想請問不要用hard code是什麼意思。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://lidemy.com/courses/390625/lectures/24510405
環境變數可以參考這個影片。
簡單說你只要看到 secret, key 等關鍵字,所使用的值通常都是敏感資訊,
這種時候如果直接寫在 code 上傳到公開的 repository 是資安大漏洞,
所以這些變數就會用環境變數的方式引入,
使用起來會像這樣。
app.use(session({
secret: process.env.SECRET
});
}, | ||
|
||
//抽獎新增 | ||
handleAddAdmin: async(req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
名稱有點怪,
這邊看起來是增加獎項但是韓式名稱是增加管理員。
@@ -0,0 +1,24 @@ | |||
./node_modules/.bin/sequelize model:generate --name User --attributes username:string,password:string,nickname:string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這些內容可以寫在 README.md 或者 docs 裡面,
讓之後的開發者可以知道這個專案的一些資訊。
不用隨便用 a.txt 這樣沒有識別性的檔案名稱。
} | ||
req.session.username = user.username | ||
req.session.userId = user.id | ||
console.log("USER 是誰",user) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log("USER 是誰",user) |
|
||
|
||
|
||
function checkIsAdmin(req, res, next) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這個 function 不要放在 index.js 裡面宣告,
讀起來比較乾淨。
app.post('/backstage', prizeController.handleAddAdmin) //後台新增 | ||
app.get('/update-lottery/:id', prizeController.updatePage) //後台更新 | ||
app.post('/update-lottery/:id', prizeController.handleUpdate) | ||
app.get('/delete-lottery/:id', prizeController.delete) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要做權限管理。
十七週作業完成
餐廳官網:
https://young-sierra-55856.herokuapp.com/
管理者帳號密碼:admin
BLOG:
https://murmuring-river-65134.herokuapp.com/