TIL028_210425

JIYOONยท2021๋…„ 4์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
28/42
post-thumbnail

๐ŸŠ ๊ฐ์ƒ

๐Ÿ“™ ์—ดํ’ˆํƒ€ ์ฝ”๋”ฉ ์‹œ๊ฐ„ 9hour
๐Ÿ‘๐Ÿผ -
๐Ÿ‘Ž๐Ÿผ -

๐Ÿš€ ๋ชฉํ‘œ

  • Udemy์—์„œ Javascript ๊ฐ•์ขŒ ์ˆ˜๊ฐ•ํ•˜๊ธฐ (417/682)
  • ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰

๐Ÿ“ฃ The Web Developer Bootcamp 2021

32. Exploring Modules & The NPM Universe

325. The all-important package.json

package.json -> contains metadata or information about this particular project or package or application
dependencies

npm.init -> easy way of making a package .json -> will be filled out with whatever answers you enter

if we install dependencies -> those dependencies will automatically be added to the dependency section of package.json
-> useful for keeping a record of what we're using and for sharing this file with other people or with myself on a different machine where I can actually go and download these dependencies in one shot.

327. Installing all dependencies for a project

download the code (node.modules ํด๋”๋Š” ์—†์Œ, ์ €์žฅ๊ณต๊ฐ„์„ ๋งŽ์ด ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ)
์ฝ”๋“œ ๋‹ค์šด๋กœ๋“œ ํ•œ ๋‹ค์Œ์— ๋‹ค์šดํ•œ ํด๋” ๋‚ด์—์„œ ์ปค๋งจ๋“œ๋ผ์ธ์— npm install ์ž…๋ ฅ
-> package.json ํŒŒ์ผ ๋ณด๊ณ  ์ž๋™์ ์œผ๋กœ ํ•„์š”ํ•œ dependencies ๋‹ค์šด๋ฐ›์Œ -> node.modules ํด๋” ๋งŒ๋“ค์–ด์ง
์•ˆ ์“ฐ๋Š” dependencies๋Š” ์ง€์›Œ์•ผ ์ฝ”๋“œ ๋‹ค์šด๋ฐ›๋Š” ์‚ฌ๋žŒ์ด ์“ธ๋ชจ์—†๋Š” ๊ฒƒ ๋‹ค์šด๋ฐ›์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ

328. Language guesser challenge

const franc = require('franc');
const langs = require('langs');
const colors = require('colors');
const input = process.argv[2];
const langCode = franc(input);
if (langCode === 'und') {
  console.log("sorry,couldn't figure it out".red);
} else {
  const language = langs.where('3', langCode);
  console.log(`Our best guess is : ${language.name}`.green);
}

33. Creating Servers with express

express is a framework for web developement for creating servers using node.

330. Introducing express

express

express is a fast, unopinionated(ํŽธํ–ฅ์ , ํŠน์ • ์šด์˜์ฒด์ œ์— ์ข…์†๋˜์ง€ ์•Š์€, ๋ฆฌ๋ˆ…์Šค, ๋งฅ, ์œˆ๋„์šฐ์—์„œ ๋ชจ๋‘ ๋™์ž‘ํ•˜๋Š”), minimalist web framewokr for Node.js
helps us build web apps
NPM package which comes with a bunch of methods and plugins that we can use to build web application and API's

  • start up a server to listen for requests
  • parse incoming requests (json -> javascript or object)
  • match those requests to particular routes
  • craft our http response and associated content

library and framework

library: you are in charge. you control the flow of the application code, and decide when to use the library

framework: control is inverted. the framework is in charge, and you are merely a participant. framework tells you where to plug in the code.

331. Our very first express app

const express = require('express');
const app = express(); //save the return value in a variable
console.dir(app); //app object has a lot of methods on it

app.use(() => {
  console.log('we got a new request');
});
//incoming request -> executed every time the app recieves a request

app.listen(3000, () => {
  console.log('listening on port');
}); //to get a server going and start listening
//3000 is a port
//localhost:3000 -> local machine

332. The request & response object

ctrl + c ์„œ๋ฒ„ ๋‹ซ์Œ
node index.js ๋ณ€๊ฒฝ์‚ฌํ•ญ ์žˆ์„ ์‹œ ์ž…๋ ฅํ•ด ์ค˜์•ผ ํ•จ, restart the server

const express = require('express');
const app = express();
console.dir(app);

app.use((req, res) => {
  console.log('we got a new request');
  res.send('hello we got your request');
});

app.listen(1397, () => {
  console.log('listening on port');
});

333. Express Routing basics

์ฐธ๊ณ ๋งํฌ: ๊น€์Šน์—ฝ
app.get -> ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹ (url์— id์™€ password๊ฐ€ ๋…ธ์ถœ๋จ)
app.post -> ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ์‹

routing: taking incoming requests and a path that is requested and matching that to some code in some response

we can't have an HTTP request that gets more than one response

const express = require('express');
const app = express();
console.dir(app);

// app.use((req, res) => {
//   console.log('we got a new request');
//   res.send('hello we got your request');
// });

app.get('/cats', (req, res) => {
  res.send('meow');
});
app.post('/cats', (req, res) => {
  res.send('post request to /cats, different from get request');
});
app.get('/dogs', (req, res) => {
  res.send('woof');
});
app.get('/', (req, res) => {
  res.send('welcome to home'); //root route
});
app.get('*', (req, res) => {
  //generic response
  res.send("I don't know that path");
});
//๋งจ ์œ„์—๋‹ค ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜ ๋ชจ๋‘ ๋ฌด์‹œ๋˜๋ฏ€๋กœ ์ฃผ์˜, route๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋งค์น˜๋˜๊ธฐ ๋•Œ๋ฌธ

app.listen(1397, () => {
  console.log('listening on port');
});

334. Express path parameters

make generic pattern
์˜ˆ์‹œ๋งํฌ: ๋ ˆ๋”ง

app.get('/r/:subreddit', (req, res) => {
  const { subreddit } = req.params;
  res.send(`<h1>Browsing the ${subreddit}</h1>`);
});
app.get('/r/:subreddit/:postId', (req, res) => {
  const { subreddit, postId } = req.params;
  res.send(`<h1>Vewing ${postId} on the ${subreddit}</h1>`);
  //: -> indicates variable
});

335. Working with Query strings

์˜ˆ์‹œ๋งํฌ: MDN

app.get('/search', (req, res) => {
  const { q } = req.query;
  res.send(`search result for ${q}`);
});
//localhost:8080/search?q=cat&color=black

336. Auto restart with Nodemon

nodemon index.js

34. Creating dynamic HTML with templating

338. What is templating

templating allows us to define a preset pattern for a webpage, that we can dynamically modify.

339. Configuring Express for EJS

app.set('view engine', 'ejs');
//express behind the scene will require the package called EJS
app.get('/', (req, res) => {
  res.render('home'); //root route
});
//you can add home.ejs, but actually we don't need it if we set the view engine to ejs
//we don't need to say views/home either, because default place is views

340. Setting the views directory

๋‹ค๋ฅธ ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ ํ•ด๊ฒฐ
-> views directory๊ฐ€ current working directory๋กœ ์„ค์ •๋ผ ์žˆ๋Š” ๊ฒƒ์„ ๋ฐ”๊พผ๋‹ค
๋””๋ ‰ํ† ๋ฆฌ ์ฃผ์†Œ๋ฅผ index.js/views๋กœ ๋ฐ”๊พธ๊ธฐ
os๋ณ„๋กœ ๋‹ฌ๋ผ์ง€๋Š” ์ฃผ์†Œ ๊ฒฝ๋กœ๋ฅผ ์ž๋™์œผ๋กœ ์ฐพ์•„์ฃผ๊ณ  ์ฃผ์†Œ ์กฐํ•ฉ์„ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ

const path = require('path');
app.set('views', path.join(__dirname, '/views'));
//taking our current directory name where the file is located index.js
//and joining the path to /views 

341. EJS interpolation syntax

<h1>homepage <%=4+5+1%> <%='hello world'.toUpperCase()%></h1>

342. Passing data to templates

app.get('/rand', (req, res) => {
  const num = Math.floor(Math.random() * 10) + 1;
  res.render('random', { rand: num });
});
//second argument is a object, key-value pairs
//num is available in my template under the name rand

343. Subreddit template demo

app.get('/r/:subreddit', (req, res) => {
  const { subreddit } = req.params;
  res.render('subreddit', { subreddit });
});
//passing information from the params from the path
//pass that through to the template called subreddit (subreddit.ejs)
//and path that through under the name of subreddit
//and rendering in subreddit.ejs

344. Conditionals in EJS

    <h1>Your random number is : <%= rand %></h1>
    <%if(rand%2===0){%>
    <h2>That is an even number</h2>
    <%} else {%>
    <h2>That is an odd number</h2>
    <%}%>
    <h3>The number is : <%= rand%2===0 ? 'EVEN':'ODD' %></h3>

345. Loops in EJS

//in index.js
app.get('/cats', (req, res) => {
  const cats = ['Blue', 'Rocket', 'Monty', 'Winston'];
  res.render('cats', { cats });
});
//in cats.ejs
    <ul>
      <% for (let cat of cats) { %>
      <li><%= cat %></li>
      <% } %>
    </ul>

346. A more complex subreddit demo

const redditData = require('/data.json')
app.get('/r/:subreddit', (req, res) => {
  const { subreddit } = req.params;
  const data = redditData[subreddit];
  if (data) {
    res.render('subreddit', { ...data });
  } else {
    res.render('notfound', { subreddit });
  }
  res.render('subreddit', { ...data });
  //this allows me to do access individual properties like description
});

//in subreddit.ejs
    <% for(let post of posts){ %>
    <article>
      <p><%= post.title %> - <b><%= post.author %></b></p>
      <% if (post.img) { %>
      <img src="<%=post.img%>" alt="" />
      <% } %>
    </article>
    <% }%>

347. Serving Static assets in Express

app.use(express.static('public'))
//('css') ('js') ํด๋”๋ณ„๋กœ ๊ฐ€๋Šฅํ•จ 

app.use(express.static(path.join(__dirname, 'public')));
//absolute path based upon wherever index.js file is 

348. Bootstrap + Express

    <link rel="stylesheet" href="/css/bootstrap.min.css" />
    <script src="/js/jquery-3.6.0.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>

349. EJS & partials

<%- include('partials/head') %>

<%- will be treated as HTML
<%= won't be treated as HTML

35. Defining RESTful Routes

351. Get vs Post requests

Get

Used to retrieve information
Data is sent via query string
Information is plainly visible in the URL
Limited amount of data can be sent

Post

Used to post data to the server
Used to write/create/update
Data is sent via request body, not a query string
Can send any sort of data(JSON)

์ฐธ๊ณ ๋งํฌ: ์ƒํ™œ์ฝ”๋”ฉ

0๊ฐœ์˜ ๋Œ“๊ธ€