TIL035_210510

JIYOONΒ·2021λ…„ 5μ›” 10일
0

TIL

λͺ©λ‘ 보기
35/42
post-thumbnail

🍊 감상

πŸ“™ μ—΄ν’ˆνƒ€ μ½”λ”© μ‹œκ°„ 6hour
πŸ‘πŸΌ -
πŸ‘ŽπŸΌ -

πŸš€ λͺ©ν‘œ

Udemy : The web developer bootcamp 2021 κ°•μ’Œ μˆ˜κ°• (501/682)
개인 ν”„λ‘œμ νŠΈ 진행
Udemy : Javascript algorithms and data structures κ°•μ’Œ μˆ˜κ°• (11/249)

πŸ“£ The Web Developer Bootcamp 2021

40. Middleware: The Key To Express

417. More Middleware Practice

//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

Setting Up A 404 Route

κ³΅μ‹λ¬Έμ„œ: expressjs.com

app.use((req, res) => {
  res.statuse(404).send('NOT FOUND');
});
//λ§ˆμ§€λ§‰μ— λ„£μ–΄μ•Ό κ·Έμ „ route 쀑 λ§€μΉ­λ˜λŠ” 것 없을 λ•Œ 싀행됨
//match any request

419. Password Middleware Demo (not real auth)

app.use((req, res, next) => {
  const { password } = req.query;
  if (password === 'chckennugget') {
    next();
  }
  res.send('sorry you need a password');
});

420. Protecting Specific Routes

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');
});

421. A New EJS Tool For Routes

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 μ‚½μž…ν•˜κ±°λ‚˜

422-429. Bootstrap5! Boilerplate

42. Handling Errors In Express Apps

431. Express' Built-In Error Handler

throw new Error('Password required')

432. Defining Custom Error Handlers

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 μ—­ν• 

433. Our Custom Error Class

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);
});

434. Handling Async Errors

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 });
});

435. Handling More Async Errors

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
  }
});

436. Defining An Async Utility

0개의 λŒ“κΈ€