π μ΄νν μ½λ© μκ° 6hour
ππΌ -
ππΌ -
Udemy : The web developer bootcamp 2021 κ°μ’ μκ° (501/682)
κ°μΈ νλ‘μ νΈ μ§ν
Udemy : Javascript algorithms and data structures κ°μ’ μκ° (11/249)
//Create middleware function
app.use((req, res, next) => {
console.log(req.method.toUpperCase());
next();
}); //GET
app.use((req, res, next) => {
console.log(req.method.toUpperCase(), req.path);
next();
}); //GET /dogs
app.use((req, res, next) => {
req.method = 'GET';
console.log(req.method, req.path);
next();
}); //make every request to get request
app.use((req, res, next) => {
req.requestTime = Date.now();
console.log(req.method, req.path);
next();
}); //GET /dogs
app.use((req, res) => {
res.statuse(404).send('NOT FOUND');
});
//λ§μ§λ§μ λ£μ΄μΌ κ·Έμ route μ€ λ§€μΉλλ κ² μμ λ μ€νλ¨
//match any request
app.use((req, res, next) => {
const { password } = req.query;
if (password === 'chckennugget') {
next();
}
res.send('sorry you need a password');
});
const verifyPassword = (req, res, next) => {
const { password } = req.query;
if (password === 'chckennugget') {
next();
}
res.send('sorry you need a password');
};
app.get('/secret', verifyPassword, (req, res) => {
res.send('My secret is');
});
npm i ejs-mate
const ejsMate = require('ejs-mate');
app.engine('ejs', ejsMate);
-> define layout file
new directory -> layouts
new file(in layouts) -> boilerplate.ejs
index.ejs μμ body λΆλΆλ§ λ¨κΈ°κ³ 맨 μμ€μ
<%layout('layouts/boilerplate')%>
boilerplate.ejsμμλ μνλ μ€μ <%- body %>
μ΄λ°μμΌλ‘ μνλ λ μ΄μμ λ§λ€ μ μμ, navbar μ½μ νκ±°λ
throw new Error('Password required')
Error handling middleware functino
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('something broke')
})
//we have four parameters
//no loger get that default error handling
next(err)
builtin express error handling
μ무κ²λ pass in λμ§ μμΌλ©΄ next middleware νΈμΆ
error pass in ν΄μΌ error handler μν
Make AppError.js
//in AppError.js
class AppError extends Error {
constructor(message, status) {
super();
this.message = message;
this.status = status;
}
}
module.exports = AppError;
//in another js file
throw new AppError('password required', 401);
Express is looking for a particular property on the error object called status or status code.
-> Just by making AppError class that has a status in it, It's all set up automatically
-> err.headerλ λ§μ°¬κ°μ§
app.use((err, req, res, next) => {
const { status = 500, message = 'Something went wrong' } = err;
res.status(status).send(message);
});
app.get('/products/:id', async (req, res, next) => {
const { id } = req.params;
const product = await Product.findById(id);
if (!product) {
// throw new AppError('Product not found', 401);
// async λ΄μμ μΈ λλ nextλ₯Ό λ°λμ μ¨μ€μΌ νλ€
// next μμ return μ¨μ€μΌ ν¨
return next(new AppError('Product not found', 401));
}
res.render('products/show', { product });
});
app.put('/products/:id', async (req, res, next) => {
try {
const { id } = req.params;
const product = await Product.findByIdAndUpdate(id, req.body);
res.redirect(`/products/${product._id}`);
} catch (e) {
next(e);
//catch the error and pass it to next
}
});