๐ ์ดํํ ์ฝ๋ฉ ์๊ฐ 9hour
๐๐ผ -
๐๐ผ -
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.
download the code (node.modules ํด๋๋ ์์, ์ ์ฅ๊ณต๊ฐ์ ๋ง์ด ์ฐจ์งํ๊ธฐ ๋๋ฌธ)
์ฝ๋ ๋ค์ด๋ก๋ ํ ๋ค์์ ๋ค์ดํ ํด๋ ๋ด์์ ์ปค๋งจ๋๋ผ์ธ์ npm install ์
๋ ฅ
-> package.json ํ์ผ ๋ณด๊ณ ์๋์ ์ผ๋ก ํ์ํ dependencies ๋ค์ด๋ฐ์ -> node.modules ํด๋ ๋ง๋ค์ด์ง
์ ์ฐ๋ dependencies๋ ์ง์์ผ ์ฝ๋ ๋ค์ด๋ฐ๋ ์ฌ๋์ด ์ธ๋ชจ์๋ ๊ฒ ๋ค์ด๋ฐ์ง ์์ ์ ์์
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);
}
express is a framework for web developement for creating servers using node.
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
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.
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
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');
});
์ฐธ๊ณ ๋งํฌ: ๊น์น์ฝ
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');
});
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
});
app.get('/search', (req, res) => {
const { q } = req.query;
res.send(`search result for ${q}`);
});
//localhost:8080/search?q=cat&color=black
nodemon index.js
templating allows us to define a preset pattern for a webpage, that we can dynamically modify.
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
๋ค๋ฅธ ํ์ผ ๊ฒฝ๋ก์์ ์คํํ์ ๋ ๋ฐ์ํ๋ ์๋ฌ ํด๊ฒฐ
-> 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
<h1>homepage <%=4+5+1%> <%='hello world'.toUpperCase()%></h1>
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
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
<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>
//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>
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>
<% }%>
app.use(express.static('public'))
//('css') ('js') ํด๋๋ณ๋ก ๊ฐ๋ฅํจ
app.use(express.static(path.join(__dirname, 'public')));
//absolute path based upon wherever index.js file is
<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>
<%- include('partials/head') %>
<%- will be treated as HTML
<%= won't be treated as HTML
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)